自己做了一个单向多对一的例子。
主贴类(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();
- }