2007-08-02
在scala里调用Hibernate JPA
关键字: scala hibernate java jpa虽然scala里也能直接使用hibernate逆向工程生成的entity文件,但是使用scala可以大大减少代码量。
pojo在java中的定义如下
java 代码
- @Entity
- @Table(name = "mytable", catalog = "mydb")
- public class MyTable implements java.io.Serializable {
- private int id;
- private String title;
- private String summary;
- private Date date;
- private String tags;
- public MyTable () {
- }
- public MyTable (int id, String title, Date date) {
- this.id = id;
- this.title = title;
- this.date = date;
- }
- public MyTable (int id, String title, String summary, Date date,
- String tags) {
- this.id = id;
- this.title = title;
- this.summary = summary;
- this.date = date;
- this.tags = tags;
- }
- @Id
- @Column(name = "id", unique = true, nullable = false)
- public int getId() {
- return this.id;
- }
- public void setId(int id) {
- this.id = id;
- }
- @Column(name = "title", nullable = false, length = 256)
- public String getTitle() {
- return this.title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- @Column(name = "summary")
- public String getSummary() {
- return this.summary;
- }
- public void setSummary(String summary) {
- this.summary = summary;
- }
- @Temporal(TemporalType.DATE)
- @Column(name = "date", nullable = false, length = 0)
- public Date getDate() {
- return this.date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- @Column(name = "tags")
- public String getTags() {
- return this.tags;
- }
- public void setTags(String tags) {
- this.tags = tags;
- }
- }
Scala由于有内置的BeanProperty支持,上面的代码缩减为下面的,一切setter/getter函数都免了,当然愿意手工加也可以
scala 代码
- @Entity
- @Table{val name = "mytable", val catalog = "mydb"}
- class MyTable extends java.io.Serializable {
- @Id
- @Column{val name = "id", val unique = true, val nullable = false}
- @BeanProperty
- var id: int = _
- @Column{val name = "title", val nullable = false, val length = 256}
- @BeanProperty
- var title: String = _
- @Column{val name = "summary"}
- @BeanProperty
- var summary: String = _
- @Temporal(TemporalType.DATE)
- @Column{val name = "date", val nullable = false, val length = 0}
- @BeanProperty
- var date: Date = _
- @Column{val name = "tags"}
- @BeanProperty
- var tags: String = _
- def this(id: int, title: String, date: Date) = {
- this();
- this.id = id;
- this.title = title;
- this.date = date;
- }
- def this(id: int, title: String, summary: String, date: Date,
- tags: String) = {
- this();
- this.id = id;
- this.title = title;
- this.summary = summary;
- this.date = date;
- this.tags = tags;
- }
- }
访问也很简单
scala 代码
- val emf = Persistence.createEntityManagerFactory("helloworld")
- val em = emf.createEntityManager()
- var tests = em.createQuery(
- "from MyTable"
- ).getResultList()
- // 这里将java.util.List转换为scala的List,方便处理
- val testsList = new scala.collection.jcl.BufferWrapper[MyTable] {
- override def underlying = tests
- }
- for (test < - testsList) {
- println(test)
- println(test.title)
- println(test.summary)
- }
- 浏览: 10110 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
在scala里调用Hibernate ...
是在JVM上的一种函数式语言,和java可以互相调用,http://www.sc ...
-- by fakechris -
scala学习笔记(1)
用了 Java 的实现描述语法我知道;不过利用了虚类,确实有点新意啊。
-- by Lich_Ray -
scala学习笔记(1)
实际上String=>Unit 是一个scala的trait,相当于java的接 ...
-- by fakechris -
scala学习笔记(1)
var fn = new ( String=>Unit ) { def ap ...
-- by Lich_Ray -
python的小工具,把hibern ...
嗯,输入太工整了正则表达式也就能处理这么工整的代码了,格式比较乱的还是要上ast ...
-- by fakechris






评论排行榜