首先贴上一个MkYong的例子
stock.java
1 package com.mkyong.stock; 2 3 import javax.persistence.CascadeType; 4 import javax.persistence.Column; 5 import javax.persistence.Entity; 6 import javax.persistence.FetchType; 7 import javax.persistence.GeneratedValue; 8 import static javax.persistence.GenerationType.IDENTITY; 9 import javax.persistence.Id; 10 import javax.persistence.OneToOne; 11 import javax.persistence.Table; 12 import javax.persistence.UniqueConstraint; 13 14 @Entity 15 @Table(name = "stock", catalog = "mkyongdb", uniqueConstraints = { 16 @UniqueConstraint(columnNames = "STOCK_NAME"), 17 @UniqueConstraint(columnNames = "STOCK_CODE") }) 18 public class Stock implements java.io.Serializable { 19 20 private Integer stockId; 21 private String stockCode; 22 private String stockName; 23 private StockDetail stockDetail; 24 25 public Stock() { 26 } 27 28 public Stock(String stockCode, String stockName) { 29 this.stockCode = stockCode; 30 this.stockName = stockName; 31 } 32 33 public Stock(String stockCode, String stockName, StockDetail stockDetail) { 34 this.stockCode = stockCode; 35 this.stockName = stockName; 36 this.stockDetail = stockDetail; 37 } 38 39 @Id 40 @GeneratedValue(strategy = IDENTITY) 41 @Column(name = "STOCK_ID", unique = true, nullable = false) 42 public Integer getStockId() { 43 return this.stockId; 44 } 45 46 public void setStockId(Integer stockId) { 47 this.stockId = stockId; 48 } 49 50 @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10) 51 public String getStockCode() { 52 return this.stockCode; 53 } 54 55 public void setStockCode(String stockCode) { 56 this.stockCode = stockCode; 57 } 58 59 @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20) 60 public String getStockName() { 61 return this.stockName; 62 } 63 64 public void setStockName(String stockName) { 65 this.stockName = stockName; 66 } 67 68 @OneToOne(fetch = FetchType.LAZY, mappedBy = "stock", cascade = CascadeType.ALL) 69 public StockDetail getStockDetail() { 70 return this.stockDetail; 71 } 72 73 public void setStockDetail(StockDetail stockDetail) { 74 this.stockDetail = stockDetail; 75 } 76 77 }
StockDetail.java
1 package com.mkyong.stock; 2 3 import java.util.Date; 4 import javax.persistence.Column; 5 import javax.persistence.Entity; 6 import javax.persistence.FetchType; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.OneToOne; 10 import javax.persistence.PrimaryKeyJoinColumn; 11 import javax.persistence.Table; 12 import javax.persistence.Temporal; 13 import javax.persistence.TemporalType; 14 import org.hibernate.annotations.GenericGenerator; 15 import org.hibernate.annotations.Parameter; 16 17 @Entity 18 @Table(name = "stock_detail", catalog = "mkyongdb") 19 public class StockDetail implements java.io.Serializable { 20 21 private Integer stockId; 22 private Stock stock; 23 private String compName; 24 private String compDesc; 25 private String remark; 26 private Date listedDate; 27 28 public StockDetail() { 29 } 30 31 public StockDetail(Stock stock, String compName, String compDesc, 32 String remark, Date listedDate) { 33 this.stock = stock; 34 this.compName = compName; 35 this.compDesc = compDesc; 36 this.remark = remark; 37 this.listedDate = listedDate; 38 } 39 40 @GenericGenerator(name = "generator", strategy = "foreign", 41 parameters = @Parameter(name = "property", value = "stock")) 42 @Id 43 @GeneratedValue(generator = "generator") 44 @Column(name = "STOCK_ID", unique = true, nullable = false) 45 public Integer getStockId() { 46 return this.stockId; 47 } 48 49 public void setStockId(Integer stockId) { 50 this.stockId = stockId; 51 } 52 53 @OneToOne(fetch = FetchType.LAZY) 54 @PrimaryKeyJoinColumn 55 public Stock getStock() { 56 return this.stock; 57 } 58 59 public void setStock(Stock stock) { 60 this.stock = stock; 61 } 62 63 @Column(name = "COMP_NAME", nullable = false, length = 100) 64 public String getCompName() { 65 return this.compName; 66 } 67 68 public void setCompName(String compName) { 69 this.compName = compName; 70 } 71 72 @Column(name = "COMP_DESC", nullable = false) 73 public String getCompDesc() { 74 return this.compDesc; 75 } 76 77 public void setCompDesc(String compDesc) { 78 this.compDesc = compDesc; 79 } 80 81 @Column(name = "REMARK", nullable = false) 82 public String getRemark() { 83 return this.remark; 84 } 85 86 public void setRemark(String remark) { 87 this.remark = remark; 88 } 89 90 @Temporal(TemporalType.DATE) 91 @Column(name = "LISTED_DATE", nullable = false, length = 10) 92 public Date getListedDate() { 93 return this.listedDate; 94 } 95 96 public void setListedDate(Date listedDate) { 97 this.listedDate = listedDate; 98 } 99 100 }
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mkyongdb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <mapping class="com.mkyong.stock.Stock" /> <mapping class="com.mkyong.stock.StockDetail" /> </session-factory> </hibernate-configuration>
然后是用于测试的App.java
package com.mkyong; import java.util.Date; import org.hibernate.Session; import com.mkyong.stock.Stock; import com.mkyong.stock.StockDetail; import com.mkyong.util.HibernateUtil; public class App { public static void main(String[] args) { System.out.println("Hibernate one to one (Annotation)"); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Stock stock = new Stock(); stock.setStockCode("7052"); stock.setStockName("PADINI"); StockDetail stockDetail = new StockDetail(); stockDetail.setCompName("PADINI Holding Malaysia"); stockDetail.setCompDesc("one stop shopping"); stockDetail.setRemark("vinci vinci"); stockDetail.setListedDate(new Date()); stock.setStockDetail(stockDetail); stockDetail.setStock(stock); session.save(stock); session.getTransaction().commit(); System.out.println("Done"); } }