one-to-one:由映射文件自动生成数据库,添加数据的时候,不能两个一起添加
one-to-one:由映射文件自动生成数据库,添加数据的时候,不能两个一起添加,相关文件如下:
Idcard.hbm.xml:
Java code?<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.xyy.domain.Idcard"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="foreign"> <param name="property">person</param> </generator> </id> <property name="validate" type="java.util.Date"> <column name="validate"/> </property> <!-- 配置one-to-one constrained:不配置,则不会自动生成外键列,配置了,则会生成外键列 --> <one-to-one name="person" constrained="true"/> </class> </hibernate-mapping>
Person.hbm.xml:
Java code?<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.xyy.domain.Person"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="assigned"/> </id> <property name="name" type="java.lang.String"> <column name="name"/> </property> <!-- 配置one-to-one --> <one-to-one name="idcard" /> </class> </hibernate-mapping>
TestMain.java:
Java code?Person p1=new Person(); p1.setId(2); p1.setName("scott1"); Idcard idcard=new Idcard(); idcard.setValidate(new Date()); idcard.setPerson(p1); session.save(p1); session.save(idcard);
报错:
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
Caused by: java.sql.BatchUpdateException: ORA-00928: 缺失 SELECT 关键字
用级联呀..
Java code?123456789 Person p1=new Person(); p1.setId(2); p1.setName("scott1"); Idcard idcard=new Idcard(); idcard.setValidate(new Date()); idcard.setPerson(p1); session.save(idcard);
XML/HTML code?<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.xyy.domain.Idcard"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="foreign"> <param name="property">person</param> </generator> </id> <property name="validate" type="java.util.Date"> <column name="validate"/> </property> <!-- 配置one-to-one constrained:不配置,则不会自动生成外键列,配置了,则会生成外键列 --> <one-to-one name="person" constrained="true" <SPAN style="COLOR: #ff0000">cascade="save-update"</SPAN>/> </class> </hibernate-mapping>