環境:java
Hibernate 3.3.1 Maven 3.0.4 MySQL 5.5.13 Myeclipse 8.6.1
建表語句:mysql
DROP TABLE IF EXISTS `t_card`; CREATE TABLE `t_card` ( `cardId` int(10) unsigned NOT NULL AUTO_INCREMENT, `cardNumber` char(18) NOT NULL, PRIMARY KEY (`cardId`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gb2312; INSERT INTO `t_card` VALUES ('1', '440911199008011122');
DROP TABLE IF EXISTS `t_person`; CREATE TABLE `t_person` ( `personId` int(10) unsigned NOT NULL AUTO_INCREMENT, `personName` varchar(15) NOT NULL, `cid` int(10) unsigned NOT NULL, PRIMARY KEY (`personId`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gb2312; INSERT INTO `t_person` VALUES ('1', 'fancy', '1');
Person.javasql
package com.fancy.po; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; /** * ----------------------------------------- * @文件: Person.java * @做者: fancy * @郵箱: fancyzero@yeah.net * @時間: 2012-6-10 * @描述: 實體類 * ----------------------------------------- */ /** * @Entity 聲明一個類爲實體Bean * @Table(name = "xx")指定實體類映射的表,若是表名和實體類名一致,能夠不指定 */ @Entity @Table(name = "t_person") public class Person { private Integer personId; private String personName; private Card card; /** * @Id 映射主鍵屬性,這裏採用uuid的主鍵生成策略 * @GeneratedValue —— 註解聲明瞭主鍵的生成策略。該註解有以下屬性 * strategy 指定生成的策略,默認是GenerationType. AUTO * GenerationType.AUTO 主鍵由程序控制 * GenerationType.TABLE 使用一個特定的數據庫表格來保存主鍵 * GenerationType.IDENTITY 主鍵由數據庫自動生成,主要是自動增加類型 * GenerationType.SEQUENCE 根據底層數據庫的序列來生成主鍵,條件是數據庫支持序列 * generator 指定生成主鍵使用的生成器 */ @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getPersonId() { return personId; } /** * @OneToOne:一對一關聯 * cascade:級聯,它能夠有有五個值可選,分別是: * CascadeType.PERSIST:級聯新建 * CascadeType.REMOVE : 級聯刪除 * CascadeType.REFRESH:級聯刷新 * CascadeType.MERGE : 級聯更新 * CascadeType.ALL : 以上所有四項 * @JoinColumn:主表外鍵字段 * cid:Person所映射的表中的一個字段 */ @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "cid") public Card getCard() { return card; } public String getPersonName() { return personName; } public void setPersonId(Integer personId) { this.personId = personId; } public void setPersonName(String personName) { this.personName = personName; } public void setCard(Card card) { this.card = card; } }
Card.java數據庫
package com.fancy.po; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; /** * ----------------------------------------- * @文件: Card.java * @做者: fancy * @郵箱: fancyzero@yeah.net * @時間: 2012-6-10 * @描述: 實體類 * ----------------------------------------- */ @Entity @Table(name = "t_card") public class Card { private Integer cardId; private String cardNumber; private Person person; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getCardId() { return cardId; } /** * @OneToOne:一對一關聯 * mappedBy = "card":意思是說這裏的一對一配置參考了card * card又是什麼呢?card是Person類中的getCard(),注意不是Person類中的 * card屬性,Person類中的OneToOne配置就是在getCard()方法上面配的. * 若是Person類中的getCard()方法改爲getIdCard(),其餘不變的話, * 這裏就要寫成:mappedBy = "idCard" */ @OneToOne(mappedBy = "card") public Person getPerson() { return person; } public String getCardNumber() { return cardNumber; } public void setCardId(Integer cardId) { this.cardId = cardId; } public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; } public void setPerson(Person person) { this.person = person; } }
pom.xmlapache
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.fancy</groupId> <artifactId>hibernate-annotation-on-to-one-example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hibernate-annotation-on-to-one-example</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- hibernate jar --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.3.1.ga</version> </dependency> <!-- hibernate annotation jar --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.3.1.GA</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Test.javasession
package com.fancy.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import com.fancy.po.Card; import com.fancy.po.Person; /** * ----------------------------------------- * @文件: Test.java * @做者: fancy * @郵箱: fancyzero@yeah.net * @時間: 2012-6-10 * @描述: 測試類 * ----------------------------------------- */ public class Test { public static void main(String[] args) { //讀取hibernate配置,默認讀取classpath下的hibernate.cfg.xml Configuration conf = new AnnotationConfiguration(); //構建session工廠 SessionFactory sessionFactory = conf.configure().buildSessionFactory(); //打開session Session session = sessionFactory.openSession(); //開始事務 session.beginTransaction(); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * //test1(session); //測試 1 //test2(session); //測試 2 test3(session); //測試 3 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * //提交事務 session.getTransaction().commit(); //關閉session工廠 sessionFactory.close(); //關閉session session.close(); } /** * 數據庫初始值: * personName:fancy * cardNumber:440911199008011122 */ //測試方法1 public static void test1(Session session){ Person person = (Person)session.get(Person.class, 1); //發出Person和Card的select語句 Card card = person.getCard(); System.out.println(person.getPersonName()); System.out.println(card.getCardNumber()); person.setPersonName("fancy"); //與初始值一致 card.setCardNumber("440911199008011122");//與初始值一致 session.save(person); //不發出sql語句 //session.save(card); //不發出sql語句 } //測試方法2 public static void test2(Session session){ Person person = (Person)session.get(Person.class, 1); //發出Person和Card的select語句 Card card = person.getCard(); System.out.println(person.getPersonName()); System.out.println(card.getCardNumber()); person.setPersonName("fancyzero"); //與初始值不一致 card.setCardNumber("440911199008011122");//與初始值一致 session.save(person); //發出Person的update語句,數據庫中personName被修改,不發出Card的update語句 //session.save(card); //發出Person的update語句,數據庫中personName被修改,不發出Card的update語句 } //測試方法3 public static void test3(Session session){ Person person = (Person)session.get(Person.class, 1); //發出Person和Card的select語句 Card card = person.getCard(); System.out.println(person.getPersonName()); System.out.println(card.getCardNumber()); person.setPersonName("fancy"); //與初始值不一致 card.setCardNumber("440911199008080808");//與初始值不一致 session.save(person); //同時發出Person和Card的update語句,數據庫中的數據相應被修改 //session.save(card); //同時發出Person和Card的update語句,數據庫中的數據相應被修改 } }