标签:
Hibernate中继承结构映射有三种实现方式:
package test.hibernate.hbmExtends; import java.util.Date; public class Article { private Integer id; private String title; private String content; private Date postTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getPostTime() { return postTime; } public void setPostTime(Date postTime) { this.postTime = postTime; } }
package test.hibernate.hbmExtends; public class Topic extends Article{ private int type;//精华、置顶 public int getType() { return type; } public void setType(int type) { this.type = type; } }
package test.hibernate.hbmExtends; public class Reply extends Article{ private int floor; public int getFloor() { return floor; } public void setFloor(int floor) { this.floor = floor; } }这里只有一个配置文件Article.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="test.hibernate.hbmExtends"> <!-- discriminator-value属性:用于鉴别是哪个类的一个值,表示这 个值就是这个类。 如果不写,默认为类的全限定名。 --> <class name="Article" table="article" discriminator-value="Article"> <id name="id" type="integer" column="id"> <generator class="native" /> </id> <discriminator type="string" column="class_"></discriminator> <property name="title" /> <property name="content" /> <property name="postTime" type="timestamp" /> <subclass name="Topic" discriminator-value="Topic"> <property name="type"></property> </subclass> <subclass name="Reply" discriminator-value="Reply"> <property name="floor"></property> </subclass> </class> </hibernate-mapping>
package test.hibernate.hbmExtends; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sessionFactory = new Configuration()// .configure()// .addClass(Article.class)// 添加Hibernate实体类(加载对应的映射文件) .buildSessionFactory(); @Test public void testSave() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- // 构建对象 Article article = new Article(); article.setTitle("这是一篇文章"); Topic topic = new Topic(); topic.setTitle("这是一张帖子"); Reply reply = new Reply(); reply.setTitle("这是一张回复"); // 保存 session.save(article); session.save(topic); session.save(reply); // -------------------------------------------- session.getTransaction().commit(); session.close(); } @Test public void testGet() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // 获取数据 // Article article=(Article) session.get(Article.class, 1); // Topic topic=(Topic) session.get(Topic.class, 2); // Reply reply=(Reply) session.get(Reply.class, 3); Article article = (Article) session.get(Article.class, 1); Article topic = (Article) session.get(Article.class, 2); Article reply = (Article) session.get(Article.class, 3); System.out.println(article); System.out.println(topic); System.out.println(reply); session.getTransaction().commit(); session.close(); } }表结构
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="test.hibernate.hbmExtends2"> <!-- 采用每个类一张表的方式,抽象类也对应表 --> <class name="Article" table="article2"> <id name="id" type="integer" column="id"> <generator class="native" /> </id> <property name="title" /> <property name="content" /> <property name="postTime" type="timestamp" /> <!-- 定义子类Topic --> <joined-subclass name="Topic" table="topic2"> <key column="id"></key> <property name="type"></property> </joined-subclass> <!-- 定义子类Reply --> <joined-subclass name="Reply" table="reply2"> <key column="id"></key> <property name="floor"></property> </joined-subclass> </class> </hibernate-mapping>
package test.hibernate.hbmExtends2; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sessionFactory = new Configuration()// .configure()// .addClass(Article.class)// 添加Hibernate实体类(加载对应的映射文件) .buildSessionFactory(); @Test public void testSave() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- // 构建对象 Article article = new Article(); article.setTitle("这是一篇文章"); Topic topic = new Topic(); topic.setTitle("这是一张帖子"); Reply reply = new Reply(); reply.setTitle("这是一张回复"); // 保存 session.save(article); session.save(topic); session.save(reply); // -------------------------------------------- session.getTransaction().commit(); session.close(); } @Test public void testGet() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // 获取数据 // Article article=(Article) session.get(Article.class, 1); // Topic topic=(Topic) session.get(Topic.class, 2); // Reply reply=(Reply) session.get(Reply.class, 3); Article article = (Article) session.get(Article.class, 1); Article topic = (Article) session.get(Article.class, 2); Article reply = (Article) session.get(Article.class, 3); System.out.println(article); System.out.println(topic); System.out.println(reply); session.getTransaction().commit(); session.close(); } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="test.hibernate.hbmExtends3"> <!-- 采用每个具体类一张表的形式,抽象类不对应表。 abstract默认为false,设为true表示本类不对应表 (类可以不是abstract的), 这时就会忽略table属性 --> <class name="Article" abstract="false" table="article3"> <id name="id"> <!-- 这里native生成策略不能用 --> <!-- 使用每个具体类一张表的方式时,主键生成策略不能是identity。因为在整个 继承结构中,主键值是不能重复的。 --> <generator class="hilo"> <param name="table">hi_value</param> <param name="column">next_value</param> <param name="max_lo">5</param> </generator> </id> <property name="title" /> <property name="content" /> <property name="postTime" type="timestamp" /> <!-- 子类Topic --> <union-subclass name="Topic" table="topic3"> <property name="type"></property> </union-subclass> <!-- 子类Reply --> <union-subclass name="Reply" table="reply3"> <property name="floor"></property> </union-subclass> </class> </hibernate-mapping>
版权声明:本文为博主原创文章,未经博主允许不得转载。如需转载,请注明出处:http://blog.csdn.net/lindonglian
标签:
原文地址:http://blog.csdn.net/lindonglian/article/details/46958015