Part 1-JDO/JPAは双方向、有主、一対多(one-to-many)の関係を確立する
こんにちは、第1部の「JDO/JPA Snippets That Work」へようこそ!
双方向の有主の一対の多関係を確立する
図書カタログアプリケーションを構築している場合、本と章をモデリングしたいとします.本には章が含まれており、章は本とは独立して存在しません.そのため、本を削除すると、彼の章も自動的に削除されます.また、各章のインスタンスは、この章を持つ本への参照を指していると思います.これはまさに双方向で、主がいて、一対多の関係に聞こえます.
まず私たちは私たちのモデルを構築し、その後、2つの章を持つ本をモデルにします.
JPA:
2つの章を持つ本を作成しましょう(「em」というEntityManagerを作成して閉じる人がいると仮定します)
JDO:
2つの章を持つ本を作成しましょう(「em」というEntityManagerを作成して閉じる人がいると仮定します)
英語原文:
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:
Now let's create a book with two chapters (we'll assume someone else is
creating and closing an EntityManager named 'em' for us):
JDO:
Now let's create a book with two chapters (we'll assume someone else is
creating and closing a PersistenceManager named 'pm' for us):
転載先:
http://groups.google.com/group/google-appengine-java/browse_thread/thread/54c83dc6242fd633
说明:上の中国语の翻訳は私が自分で理解できる方法で翻訳したもので、逐字翻訳ではなく、自分の参考にするだけで、あまり正确ではありません.
双方向の有主の一対の多関係を確立する
図書カタログアプリケーションを構築している場合、本と章をモデリングしたいとします.本には章が含まれており、章は本とは独立して存在しません.そのため、本を削除すると、彼の章も自動的に削除されます.また、各章のインスタンスは、この章を持つ本への参照を指していると思います.これはまさに双方向で、主がいて、一対多の関係に聞こえます.
まず私たちは私たちのモデルを構築し、その後、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
}
2つの章を持つ本を作成しましょう(「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
}
2つの章を持つ本を作成しましょう(「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
说明:上の中国语の翻訳は私が自分で理解できる方法で翻訳したもので、逐字翻訳ではなく、自分の参考にするだけで、あまり正确ではありません.