score:17

Accepted answer

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.


Related Query

More Query from same tag