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();
}
}


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

说明:上面的中文翻译是我用自己能理解的方式翻译出来的,并不是逐字翻译,只作为自己参考用,并不十分精确。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值