下面是关于hibernate主键映射的源代码,感觉很简单,但就是不知道什么问题。希望那位高手指点,非常感谢!!
有两个表:
Tusern
字段:id,name,age(id为主键,外键)
Tpassportn
字段:id,senal,ex(id为主键)

两个表的映射文件如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.qin.onetoone1.Tusern" table="Tusern" schema="dbo" catalog="book">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>    
        <property name="name" type="java.lang.String">
            <column name="name" length="50" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="age" />
        </property>
         <one-to-one name="tpassportn" class="org.qin.onetoone1.Tpassportn" cascade="all" outer-join="true">         
        </one-to-one>
    </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.qin.onetoone1.Tpassportn" table="Tpassportn" schema="dbo" catalog="book">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="foreign" >
             <param name="property">tusern</param>
            </generator>
        </id>
        <property name="senal" type="java.lang.String">
            <column name="senal" length="50" />
        </property>
        <property name="ex" type="java.lang.String">
            <column name="ex" length="50" />
        </property>
        <one-to-one name="tusern" class="org.qin.onetoone1.Tusern" constrained="true">         
        </one-to-one>
    </class>
</hibernate-mapping>
pojo类:
Tusern代码如下:

package org.qin.onetoone1;

/**
 * Tusern entity.
 *
 * @author MyEclipse Persistence Tools
 */

public class Tusern implements java.io.Serializable {

 // Fields

 private Integer id;
 private Tpassportn tpassportn;
 private String name;
 private Integer age;

 // Constructors

 /** default constructor */
 public Tusern() {
 }


 /** minimal constructor */
 public Tusern(Integer id, Tpassportn tpassportn) {
  this.id = id;
  this.tpassportn = tpassportn;
 }

 /** full constructor */
 public Tusern(Integer id, Tpassportn tpassportn, String name, Integer age) {
  this.id = id;
  this.tpassportn = tpassportn;
  this.name = name;
  this.age = age;
 }

 // Property accessors

 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public Tpassportn getTpassportn() {
  return this.tpassportn;
 }

 public void setTpassportn(Tpassportn tpassportn) {
  this.tpassportn = tpassportn;
 }

 public String getName() {
  return this.name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Integer getAge() {
  return this.age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

}

Tpassportn

package org.qin.onetoone1;

import java.io.Serializable;

public class Tpassportn implements Serializable {
 private Integer id;
 private String senal;
 private String ex;
 private Tusern tusern;
 public Tpassportn() {
  // TODO Auto-generated constructor stub
 }
 @Override
 public String toString() {
  // TODO Auto-generated method stub
  return super.toString();
 }
 
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getSenal() {
  return senal;
 }
 public void setSenal(String senal) {
  this.senal = senal;
 }
 public String getEx() {
  return ex;
 }
 public void setEx(String ex) {
  this.ex = ex;
 }
 public void setTusern(Tusern tusern) {
  this.tusern = tusern;
 }
 public Tusern getTusern() {
  return tusern;
 }
}

TusernOprate 类:

package org.qin.onetoone1;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TusernOprate implements Serializable {
 private Session session;
 public TusernOprate(){
  Configuration config = new Configuration().configure();
  SessionFactory sFactory = config.buildSessionFactory();
  this.setSession(sFactory.openSession());
 }
 public void setSession(Session session) {
  this.session = session;
 }
 public Session getSession() {
  return session;
 }
}
测试类:Test.java

package org.qin.onetoone1;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Tusern user = new Tusern();
  user.setAge(20);
  user.setName("gaoqin");
  Tpassportn passport = new Tpassportn();
  passport.setSenal("sdgsdg");
  passport.setEx("sdgdsgd");
  user.setTpassportn(passport);
  passport.setTusern(user);
  
  TusernOprate to = new TusernOprate();
//插入数据
  to.getSession().save(user);
  to.getSession().beginTransaction().commit();
 }
}
错误提示:
Hibernate: insert into book.dbo.Tusern (name, age) values (?, ?) select scope_identity()
JDBCExceptionReporter-SQL Error: 547, SQLState: 23000
JDBCExceptionReporter-[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]INSERT 语句与 COLUMN FOREIGN KEY 约束 'FK_Tusern_Tpassportn' 冲突。该冲突发生于数据库 'book',表 'Tpassportn', column 'id'。
JDBCExceptionReporter-SQL Error: 3621, SQLState: HY000
JDBCExceptionReporter-[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]语句已终止。
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [org.qin.onetoone1.Tusern]
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
 at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
 at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
 at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
 at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
 at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
 at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
 at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
 at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
 at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
 at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
 at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
 at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
 at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
 at org.qin.onetoone1.Test.main(Test.java:22)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]INSERT 语句与 COLUMN FOREIGN KEY 约束 'FK_Tusern_Tpassportn' 冲突。该冲突发生于数据库 'book',表 'Tpassportn', column 'id'。
 at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
 at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
 at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
 at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.executeInternal(Unknown Source)
 at com.microsoft.jdbc.base.BasePreparedStatement.execute(Unknown Source)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1953)
 ... 15 more