Skip to content

Instantly share code, notes, and snippets.

@ericacm
Last active June 25, 2017 06:17
Show Gist options
  • Save ericacm/4979184 to your computer and use it in GitHub Desktop.
Save ericacm/4979184 to your computer and use it in GitHub Desktop.
Example Hibernate entity in Scala
/*
* Topic Entity
*/
@Entity
@Table(uniqueConstraints = Array(new UniqueConstraint(columnNames=Array("application_id", "name"))))
@NamedQueries(Array(
new NamedQuery(name="Topic.findAllByApplication", query="from Topic where application=:application"),
new NamedQuery(name="Topic.findByApplicationAndName", query="from Topic where application=:application and name=:name")
))
class Topic {
@Id @GeneratedValue
var id: Long = _
@ManyToOne(optional=false)
var application: Application = _
var created_at: Date = _
var updated_at: Date = _
var name: String = _
var description: String = _
var email_system_id: String = _
var sms_system_id: String = _
override def toString: String = "[Topic " + id + ": " + name + "]"
}
/*
* Topic DAO
*/
object Topic {
@Autowired val domainManager: DomainManager = _
AppConfig.inject(this)
def create(application: Application, name: String, description: String): Topic = {
val topic = new Topic
topic.application = application
topic.name = name
topic.description = description
domainManager.save(topic)
topic
}
def lookup(id: Long) = domainManager.get[Topic](id)
def findAllByApplication(application: Application) = domainManager.findAll[Topic]("Topic.findAllByApplication", Map("application"->application))
def findByApplicationAndName(application: Application, name: String) =
domainManager.find[Topic]("Topic.findByApplicationAndName", Map("application"->application, "name"->name))
def delete(topic: Topic) {
domainManager.delete(topic)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment