score:17
Yes, you can use Scala for both your Spring MVC code and your Hibernate classes.
Here is an example of a Spring MVC Controller in Scala taken from one of my projects:
@Controller
class HomeController {
val log: Logger = LoggerFactory.getLogger(this.getClass.getName)
@RequestMapping(Array("/"))
def home: String = {
log.debug("HomeController::home")
"home/home"
}
}
And an example of a Hibernate domain class. Note the use of private Java collection classes for Hibernate adapted by public Scala collection classes for users of the class (helped by Scala's JavaConversions class).
@Entity
class Role {
@Id @GeneratedValue
var id: Long = _
@Index(name="role_name")
var name: String = _
var created_at: Date = _
var updated_at: Date = _
@ManyToMany
private var sub_role: java.util.Set[Role] = _
def subRoles: Set[Role] = {
if (sub_role == null) sub_role = new java.util.HashSet[Role]
sub_role
}
@ManyToMany
private var permission: java.util.Set[Permission] = _
def permissions: Set[Permission] = {
if (permission == null) permission = new java.util.HashSet[Permission]
permission
}
}
The only thing I've found that I've had to use Java for is writing @Retention(RUNTIME)
annotations (for JSR-303 validations).
I use Maven and the maven-scala-plugin with mixed Java/Scala projects.
score:3
You can use scala & java together for whatever you think each works best for.
I know hibernate will have to be in java.
This isn't necessarily true. You can use scala classes as Hibernate ORM objects. You just have to make sure that the compiled .class files end up with the same getters, setters, and default constructor that Hibernate expects.
Even your Web App module can be written in scala. The controllers just need to abide by the contract which Spring MVC expects.
In general, almost anything written in Java can be re-written in scala. Of coarse there are exceptions but don't per-maturaly limit yourself from trying. Assuming your existing code dones't do anything to crazy (i.e. black-magic reflection), it should be fine.
The interesting question here is how to get it working with Maven...
There is an existing SO question which describes using the maven-scala-plugin to get this kind of project working (multi-module with mixed scala and java resources). The only extra comment I will add to this solution is that, in my experience, we could only get it working with scala modules being dependent on java sources, not the other way around. This was because our maven config used the java compiler and scala compiler seprarately. In theory, the scala compiler can compile Java files too so if you configured it to just use one compiler for everything, you wouldn't encounter this limitation.
EDIT:
Blankman commented:
@Jesse so you have done this also then? not sure I understood the maven issue.
Yes, I have worked on a couple projects which have used mixed java and scala sources. We didn't use Scala for the Hibernate layer but we did for everything else.
The maven issue I am describing was a consequence of our maven-scala-plugin configuration. You have to configure it to know how to compile your scala files. Our configuration (probably not correct) was setup to compile java files first, using javac, and then compile scala files, using scalac. This meant that we weren't allowed to write java code which referenced scala classes because they wouldn't not be available at compile time. With the proper configuration, it should be possible to use the scalac compiler to compile all your sources together which will remove this awkward restriction. The SO question I linked to above has example configuration as well as some discussion about the compile time issues I am talking about. If you need more reference for setting up the plugin, check out the plugin's website (also linked to above) for the official doc.
Source: stackoverflow.com
Related Query
- How to effectively use Scala in a Spring MVC project?
- How to use Spring Autowired (or manually wired) in Scala object?
- How do you use play framework as a library, in a scala project
- SBT: how to use scala 2.11 libraries in scala 2.12 project
- How to make IntelliJ scala project use Scala 2.9.2 version?
- Unable to use Scala and Spring MVC 3 to return JSON
- Scala Lift - How to use 3rd party JAR files in project
- How to use Scala Virtualized in a Maven project with Scala-2.10?
- How to use Gatling in a Scala 3 project
- How do we use spring jdbctemplate in scala programming
- how to use .C files in Scala project
- How to make use of Delta Lake on a regular Scala project on IDE
- How to use properties in spark scala maven project
- How can I use scala sources from a different location in a sbt project and also make it work with IntelliJ IDEA?
- how use import a scala project created with sbt in eclipse?
- How do I use Spring Expression Language for an Array in an annotation with Scala
- How to add a scala dependency to a Java project in intelliJ that use maven
- how to use spring scala to decouple?
- How to use third party libraries with Scala REPL?
- Scala type keyword: how best to use it across multiple classes
- How to fully clean, re-resolve and rebuild a Scala sbt-managed project in IDEA?
- How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?
- How can I use a Scala singleton object in Java?
- How to use MySQL JDBC driver in an SBT Scala project?
- How to use IntelliJ with Play Framework and Scala
- How to use scala trait with `self` reference?
- How do I find the correct Maven archetype project for developing with Scala in Eclipse?
- How to use a Scala class inside Pyspark
- How to Use both Scala and Python in a same Spark project?
- How to use mutable collections in Scala
More Query from same tag
- How to implement the loop function already implemented in python with scala
- how to hide the values of playframework session?
- Dynamic GroupBy and count using Spark Dataframes/Datasets
- Replace the values based on condition spark
- How can I save an RDD into HDFS and later read it back?
- Akka stream not processing all the lines from my CSV file
- Scala - Most elegant way of initialising values inside array that's already been declared?
- Spark Scala case class with array and map datatype
- Best replacement of .deep method in Scala 2.13
- Is there an equivalent to Colander/DictShield for Java/Scala?
- scala Future to run sequential jobs
- Do we really 'extend' traits in scala?
- Merging the datetime ranges at the end of the day and start of the next day
- How to deeply compare two js like objects in scala.js?
- Reducing a list of futures
- Booyer-Moore or similar in Java or Scala Libraries
- How can I exclude a specific type from a TypeClass resolution?
- sbt-assembly: How do I include the static files in src/main/webapp
- Any with type context bound
- Scala : how to keep only paths containing a full list of points
- Why is an atomic block in Scala run twice when exception gets thrown?
- Is there a way to restart an akka actor
- What is the difference between Behaviors.setup and Behaviors.receive in Akka Typed?
- Is it compulsory in scala to have _= in setter
- How to infer correct element type for empty invariant collection (e.g. Set)
- Play 2.1-RC1 reverse routing not compiling
- How to cast a HashMap as a subclass in scala?
- How to manually create a class with a configuration for testing
- Spark - how to handle with lazy evaluation in case of iterative (or recursive) function calls
- Idiomatic syntax for Option when the meaningful value is None