Part3 - update一个双向有主的child

Hello again and welcome to Episode 3 of JDO/JPA Snippets That Work. Today's episode is called......

Updating A Bidrectional Owned One-To-Many With A New Child

All the way back in episode one we demonstrated how to create both a parent and a child of a bidirectional, owned, one-to-many relationship
at the same time. This week we're going to see how to add a child to an existing parent. We'll use the same model objects we used in episode one:

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 assume we've already created a book with a few chapters in the datastore and we want to add a brand new chapter to a Book with a given id (we'll assume someone else is creating and closing an EntityManager named 'em' for us):

public void addChapterToBook(EntityManager em, Key bookKey, Chapter chapter) {
em.getTransaction().begin();
try {
Book b = em.find(Book.class, bookKey);
if (b == null) {
throw new RuntimeException("Book " + bookKey + " not found!");
}
b.getChapters().add(chapter);
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")
@Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="id asc"))
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 assume we've already created a book with a few chapters in the datastore and we want to add a brand new chapter to a Book with a given id (we'll assume someone else is creating and closing a PersistenceManager named 'pm' for us):


public void addChapterToBook(PersistenceManager pm, Key bookKey, Chapter chapter) {
pm.currentTransaction().begin();
try {
// throws a runtime exception if book is not found
Book b = pm.getObjectById(Book.class, bookKey);
b.getChapters().add(chapter);
pm.currentTransaction().commit();
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
}
}

--------------------------------

The interesting thing about both of these examples is that we're not making any explicit calls to save the new Chapter. We look up the Book identified by the Key that was passed into the function and then we manipulate the persistent state of the object by manipulating the POJO that was returned by em.fetch/pm.getObjectById. JPA and JDO both have mechanisms that allow them to monitor the objects that you've looked up for changes. Ever wonder what exactly the enhancer is doing to your classes? It's adding hooks so that the persistence framework gets notified when things change (among other things). This allows JPA and JDO to automatically flush your changes to the datastore when you commit your transaction. If you wanted to modify the title of the Book or the number of pages in an existing Chapter the approach would be exactly the same: Start a transaction, look up the Book, make your changes, commit your transaction. Whether you're using JPA or JDO your changes will be persisted for you without any explicit calls to change the persistent state. This is a prime example of how JPA and JDO facilitate "transparent persistence."

转载自:[url]http://groups.google.com/group/google-appengine-java/browse_thread/thread/28d9c3b99df25e43#[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值