Jpa単方向多対一
23044 ワード
自分で一方向多対一の例を作った.
主贴クラス(1)----回帖クラス(m)
Topic(1)------Feedback(m)
persistence.xml
TopicEntity
Feedback
テストクラス
主贴クラス(1)----回帖クラス(m)
Topic(1)------Feedback(m)
persistence.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
- <persistence-unit name="JpaTest" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <class>com.entity.Topic</class>
- <class>com.entity.Feedback</class>
- <exclude-unlisted-classes>true</exclude-unlisted-classes>
- <properties>
- <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
- <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
- <property name="hibernate.connection.username" value="ningnian"/>
- <property name="hibernate.connection.password" value="ningningx"/>
- <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
- <property name="hibernate.show_sql" value="true" />
- <property name="hibernate.hbm2ddl.auto" value="update"/>
- </properties>
- </persistence-unit>
- </persistence>
TopicEntity
- package com.entity;
-
- import java.io.Serializable;
-
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
-
- @Entity
- @Table
- public class Topic implements Serializable {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- private int id;
- private String author;
- private String title;
- private String context;
-
- @Id
- @Column
- @GeneratedValue(strategy = GenerationType.AUTO)
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- @Column(length = 20)
- public String getAuthor() {
- return author;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- @Column(length = 20)
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- @Column(length = 20)
- public String getContext() {
- return context;
- }
-
- public void setContext(String context) {
- this.context = context;
- }
-
- }
Feedback
- package com.entity;
-
- import java.io.Serializable;
-
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
-
- @Entity
- public class Feedback implements Serializable {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- private int id;
- private String author;
- private String context;
- private Topic topics;
-
- @Id
- @Column
- @GeneratedValue(strategy = GenerationType.AUTO)
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- @Column
- public String getAuthor() {
- return author;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- @Column
- public String getContext() {
- return context;
- }
-
- public void setContext(String context) {
- this.context = context;
- }
-
- /**
- * cascade = CascadeType.MERGE-- cascade=CascadeType.PERSIST--
- * cascade=CascadeType.REMOVE-- cascade=CascadeType.REFRESH--
- * -- , 。 , fetch =
- * FetchType.LAZY-- 。 fetch = FetchType.EAGER-- optional--boolean
- * , optional=false,
- */
- @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, optional = false)
- /**
- * @JoinColumn(name = "top_id")
- */
- @JoinColumn(name = "top_id")
- public Topic getTopics() {
- return topics;
- }
-
- public void setTopics(Topic topics) {
- this.topics = topics;
- }
-
- }
テストクラス
- @Test
- public void testFeedback() {
- EntityManagerFactory factory = Persistence
- .createEntityManagerFactory("JpaTest");
- EntityManager manager = factory.createEntityManager();
-
- Topic topic = new Topic();
- topic.setTitle("test");
- topic.setContext("this is test");
- topic.setAuthor("ning");
-
- Feedback fk = new Feedback();
- fk.setAuthor("nian");
- fk.setContext("________");
- fk.setTopics(topic);
-
- manager.getTransaction().begin();
- manager.persist(fk);
- manager.getTransaction().commit();
-
- }