`
iamxiaole
  • 浏览: 19326 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Part1 - JDO/JPA建立一个双向,有主,一对多(one-to-many)的关系

阅读更多
你好,欢迎来到第一部分的"JDO/JPA Snippets That Work"!

建立一个双向的有主的一对多关系

假设你正在建立一个图书目录应用程序,你想为书和章节建模:书本包含章节,章节不能独立于书本存在,所以如果你删除一本书,他的章节也会跟着自动删除。同时你想每一个章节的实例都一个指向拥有这个章节的书本的引用。听起来,这正是双向的,有主的,一对对多的关系。

首先我们将建立我们的模型,之后我将示范建立一本带有2个章节的书。

JPA:
@Entity 
public class Book { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key id; 
    private String title; 
    @OneToMany(mappedBy = "book", cascade = CascadeType.ALL) 
    private List<Chapter> chapters = new ArrayList<Chapter>(); 
    // getters and setters 
} 

@Entity 
public class Chapter { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key id; 
    private String title; 
    private int numPages; 
    @ManyToOne(fetch = FetchType.LAZY) 
    private Book book; 
    // getters and setters 
} 


现在让我们建立一个带有两个章节的书本(我们假设有人已经为我们建立和关闭一个叫'em'的EntityManager)

Book b = new Book(); 
b.setTitle("JPA 4eva"); 
Chapter c1 = new Chapter(); 
c1.setTitle("Intro"); 
c1.setNumPages(10); 
b.getChapters().add(c1); 
Chapter c2 = new Chapter(); 
c2.setTitle("Configuration"); 
c2.setNumPages(9); 
b.getChapters().add(c2); 
em.getTransaction().begin(); 
try { 
    em.persist(b); 
    em.getTransaction().commit(); 
} finally { 

    if (em.getTransaction().isActive()) { 
        em.getTransaction().rollback(); 
    } 
} 


JDO:
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = 
"true") 
public class Book { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key id; 
    private String title; 
    @Persistent(mappedBy = "book") 
    @Element(dependent = "true") 
    private List<Chapter> chapters = new ArrayList<Chapter>(); 
    // getters and setters 
} 

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = 
"true") 
public class Chapter { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key id; 
    private String title; 
    private int numPages; 
    @Persistent 
    private Book book; 
    // getters and setters 
} 

现在让我们建立一个带有两个章节的书本(我们假设有人已经为我们建立和关闭一个叫'em'的EntityManager)

Book b = new Book(); 
b.setTitle("JDO 4eva"); 
Chapter c1 = new Chapter(); 
c1.setTitle("Intro"); 
c1.setNumPages(10); 
b.getChapters().add(c1); 
Chapter c2 = new Chapter(); 
c2.setTitle("Configuration"); 
c2.setNumPages(9); 
b.getChapters().add(c2); 
pm.currentTransaction().begin(); 
try { 
    pm.makePersistent(b); 
    pm.currentTransaction().commit(); 
} finally { 

    if (pm.currentTransaction().isActive()) { 
        pm.currentTransaction().rollback(); 
    } 
}


英语原文:
Hello hello and welcome to the very first installment of JDO/JPA Snippets
That Work!

Creating A Bidrectional Owned One-To-Many

Suppose you're building a book catalog application and you want to model
books and chapters.  Books contain chapters.  A chapter cannot exist without
a book, so if you delete a book you want its chapters automatically deleted
along with it.  You also want to each chapter to have a reference to the
book that owns it.  Sounds like a bidrectional, owned, one-to-many
relationship is just the thing.  First we'll set up our model objects and
then we'll add some code to create a Book with 2 Chapters.

JPA:
@Entity 
public class Book { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key id; 
    private String title; 
    @OneToMany(mappedBy = "book", cascade = CascadeType.ALL) 
    private List<Chapter> chapters = new ArrayList<Chapter>(); 
    // getters and setters 
} 

@Entity 
public class Chapter { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Key id; 
    private String title; 
    private int numPages; 
    @ManyToOne(fetch = FetchType.LAZY) 
    private Book book; 
    // getters and setters 
} 


Now let's create a book with two chapters (we'll assume someone else is
creating and closing an EntityManager named 'em' for us):

Book b = new Book(); 
b.setTitle("JPA 4eva"); 

Chapter c1 = new Chapter(); 
c1.setTitle("Intro"); 
c1.setNumPages(10); 
b.getChapters().add(c1); 

Chapter c2 = new Chapter(); 
c2.setTitle("Configuration"); 
c2.setNumPages(9); 
b.getChapters().add(c2); 

em.getTransaction().begin(); 
try { 
    em.persist(b); 
    em.getTransaction().commit(); 
} finally { 

    if (em.getTransaction().isActive()) { 
        em.getTransaction().rollback(); 
    } 
} 


JDO:
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = 
"true") 
public class Book { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key id; 
    private String title; 
    @Persistent(mappedBy = "book") 
    @Element(dependent = "true") 
    private List<Chapter> chapters = new ArrayList<Chapter>(); 
    // getters and setters 
} 

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = 
"true") 
public class Chapter { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key id; 
    private String title; 
    private int numPages; 
    @Persistent 
    private Book book; 
    // getters and setters 
} 


Now let's create a book with two chapters (we'll assume someone else is
creating and closing a PersistenceManager named 'pm' for us):

Book b = new Book(); 
b.setTitle("JDO 4eva"); 

Chapter c1 = new Chapter(); 
c1.setTitle("Intro"); 
c1.setNumPages(10); 
b.getChapters().add(c1); 

Chapter c2 = new Chapter(); 
c2.setTitle("Configuration"); 
c2.setNumPages(9); 
b.getChapters().add(c2); 

pm.currentTransaction().begin(); 
try { 
    pm.makePersistent(b); 
    pm.currentTransaction().commit(); 
} finally { 

    if (pm.currentTransaction().isActive()) { 
        pm.currentTransaction().rollback(); 
    }
} 


转载自:http://groups.google.com/group/google-appengine-java/browse_thread/thread/54c83dc6242fd633

说明:上面的中文翻译是我用自己能理解的方式翻译出来的,并不是逐字翻译,只作为自己参考用,并不十分精确。
分享到:
评论

相关推荐

    datanucleus-api-jdo-3.2.6-API文档-中文版.zip

    赠送jar包:datanucleus-api-jdo-3.2.6.jar; 赠送原API文档:datanucleus-api-jdo-3.2.6-javadoc.jar; 赠送源代码:datanucleus-api-jdo-3.2.6-sources.jar; 赠送Maven依赖信息文件:datanucleus-api-jdo-3.2.6....

    datanucleus-api-jdo-4.2.1-API文档-中英对照版.zip

    赠送jar包:datanucleus-api-jdo-4.2.1.jar; 赠送原API文档:datanucleus-api-jdo-4.2.1-javadoc.jar; 赠送源代码:datanucleus-api-jdo-4.2.1-sources.jar; 赠送Maven依赖信息文件:datanucleus-api-jdo-4.2.1....

    datanucleus-api-jdo-3.2.6.jar )

    datanucleus-api-jdo-3.2.6.jar )

    ssh+mysql55jar包集合

    /xscjManager/WebContent/WEB-INF/lib/spring-jdo.jar /xscjManager/WebContent/WEB-INF/lib/spring-jms.jar /xscjManager/WebContent/WEB-INF/lib/spring-jmx.jar /xscjManager/WebContent/WEB-INF/lib/spring-jpa....

    spring-jdo.jar

    对JDO 1.0/2.0的支持。外部依赖spring-jdbc, JDO API, (spring-web)。

    DataNucleus JPA/JDO访问mysql示例

    Java Persistence API (JPA) 是Java平台上的一个标准,用于管理和持久化Java对象到关系数据库。JPA提供了一种模型化的面向对象的方式来处理数据库操作,使得开发者无需编写大量SQL代码即可实现数据的增删改查。 **...

    xdoclet-plugin-jdo-1.0.3.zip

    Java Data Objects(JDO)是Java平台上的一个标准接口,它提供了一种透明的对象/关系映射机制,使得开发者可以以面向对象的方式操作数据库,而无需直接编写SQL语句。JDO通过API将Java对象与数据库中的记录进行映射,...

    jdo2-api-2.3-ec hive hdfs 所需jar

    JDO2-API-2.3-EC是Java对象持久化的一个重要实现,它定义了数据存储和检索的接口,使得对象可以被透明地存储到关系数据库、NoSQL数据库甚至是对象数据库中。此版本引入了若干改进和新特性,以提升性能和可扩展性,...

    jdo2-api jdo2-api

    jdo2-api jdo2-api jdo2-api jdo2-api

    JavaEE源代码 spring-jdo

    JavaEE源代码 spring-jdoJavaEE源代码 ...jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdoJavaEE源代码 spring-jdo

    querydsl-jpa-codegen-3.3.3.zip

    Querydsl 是一个强大的Java查询语言库,它提供了一种类型安全的方式来表达SQL、JDO和JPA查询。在3.3.3版本中,Querydsl 提供了JPA Codegen工具,这是一款代码生成器,能够自动生成基于Querydsl的JPA查询实体类。通过...

    datanucleus-jdo-jca-5.1.0-release.zip

    DataNucleus是一个开源的对象关系映射(ORM)框架,它允许开发人员将Java对象持久化到各种数据存储。JDO是Java平台上的一个标准接口,它提供了一种方式来透明地访问和管理持久性数据。JCA则是Java EE平台的一部分,...

    querydsl-jpa-2.0.8.zip

    Querydsl是一个强大的Java查询工具包,它提供了类型安全的查询API,可以用于JPA、SQL、JDO等多种数据访问技术。在Querydsl JPA中,你可以利用Java对象来构建复杂的JPQL(Java Persistence Query Language)查询,...

    gaedo-google-jdo-0.2.24.zip

    【标题】"gaedo-google-jdo-0.2.24.zip" 提供的是 Google 的 Java Data Objects (JDO) 库的一个版本,这是一款针对关系数据库的数据持久化框架。Google JDO 允许开发者使用面向对象的方式操作数据库,简化了数据库...

    springdata-jpa-querydsl

    Querydsl是一个用于创建类型安全查询的Java库,它支持JPA、JDO、SQL等多种持久化技术。Querydsl通过代码生成,将查询表达式转化为可读性强的Java对象,避免了字符串拼接造成的潜在错误,提高了代码质量。 三、...

    J2EE Persistence Options - JDO, Hibernate and EJB 3.0.pdf

    ### J2EE持久化选项:JDO、Hibernate与EJB 3.0 #### 概述 在《J2EE Persistence Options - JDO, Hibernate and EJB 3.0》这篇文档中,作者Sridhar Reddy深入探讨了Java 2 Platform, Enterprise Edition (J2EE)环境...

    jdo-api-3.0.1-API文档-中英对照版.zip

    赠送jar包:jdo-api-3.0.1.jar; 赠送原API文档:jdo-api-3.0.1-javadoc.jar; 赠送源代码:jdo-api-3.0.1-sources.jar; 赠送Maven依赖信息文件:jdo-api-3.0.1.pom; 包含翻译后的API文档:jdo-api-3.0.1-javadoc-...

    db-jdo-site:Apache JDO项目

    创建一个新的子文件夹docs / api32 / jdo-api-3.2-javadoc 在子文件夹中解压缩javadoc jar 在src / main / asciidoc下编辑javadoc.adoc并创建一个新的“ JDO 3.2 javadoc”部分。 添加两个链接:

    javax.jdo-3.2.0-m3-API文档-中英对照版.zip

    赠送jar包:javax.jdo-3.2.0-m3.jar; 赠送原API文档:javax.jdo-3.2.0-m3-javadoc.jar; 赠送源代码:javax.jdo-3.2.0-m3-sources.jar; 赠送Maven依赖信息文件:javax.jdo-3.2.0-m3.pom; 包含翻译后的API文档:...

Global site tag (gtag.js) - Google Analytics