hibernate 一对一关联报错,请高手指点! Could not synchronize database state with session 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Could not synchronize database state with session 这句话到底是什么意思啊,我应该如何处理呢? 刚在网上看了一片文章http://java.ccidnet.com/art/3737/20031115/460499_1.html我将我的程序中的主键生成方式改为 uuid.hex程序就没有问题,可以直接保存为什么主键是assigned时不可以呢? 我定义了person和passport两个类,它们是一对一关联,在personBO中写了测试程序以下是我的代码:Person.javapackage model;import java.io.Serializable;public class Person implements Serializable { String id; String name; int age; Passport passport; TGroup group; public Person(){ id="1"; name=""; age=0; } public Passport getPassport() { return passport; } public void setPassport(Passport passport) { this.passport = passport; } public int getAge() { return age; } public TGroup getGroup() { return group; } public void setGroup(TGroup group) { this.group = group; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; }}Passport.javapackage model;import java.io.Serializable;public class Passport implements Serializable{ String id; String destination; Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public Passport() { } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public String getId() { return id; } public void setId(String id) { this.id = id; }}映射文件:person.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping package="model"> <class name="Person" table="person" dynamic-update="false"> <id name="id" column="id" type="string" unsaved-value="null"> <generator class="uuid.hex" /> </id> <property name="name" type="string" column="name" /> <property name="age" type="int" column="age" /> <one-to-one name="passport" class="Passport" cascade="all" outer-join="true" /> <!-- many-to-one name="group" class="TGroup" column="group_id" unique="true"/--> </class> <class name="Passport" table="passport" dynamic-update="false"> <id name="id" column="id" type="string" unsaved-value="null"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <property name="destination" type="string" column="destination" /> <one-to-one name="person" class="Person" constrained="true" /> </class> <class name="TGroup" table="tgroup"> <id name="id" column="id" type="int"> <generator class="native"/> </id> <property name="name" type="string" column="name"/> <!-- one-to-one name="person" class="Person" property-ref="group"/--> </class></hibernate-mapping>测试程序personBOpackage bo;import model.TGroup;import model.Passport;import model.Person;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.Transaction;import net.sf.hibernate.cfg.Configuration;public class PersonBO { Configuration cfg=null; SessionFactory sf=null; public PersonBO()throws Exception{ cfg=new Configuration().configure(); sf=cfg.buildSessionFactory(); } public void test() throws HibernateException{ Person person=new Person(); person.setId("9"); person.setAge(25); person.setName("gyt_6"); Passport ps=new Passport(); ps.setDestination("England"); person.setPassport(ps); ps.setPerson(person); Session session=sf.openSession(); Transaction ts=session.beginTransaction(); session.save(person); ts.commit(); session.flush(); session.close(); } public static void main(String[] args) { try{ PersonBO personBO=new PersonBO(); personBO.test(); } catch(Exception e){ e.printStackTrace(); } }} 你用的hibernate配置文件是properties还是xml,需要在config.addClass 我用的是xml,hibernate配置文件应该不会有问题,因为我的person在不和passport关联的情况下是可以保存的,只在关联保存是有问题. 请检查hibernate.cfg.xml中connection.driver_class是不是对应你的数据库的类 下面是我的hibernate的配置文件:<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property> <property name="jdbc.fetch_size">20</property> <property name="jdbc.batch_size">30</property> <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> <property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test;SelectMethod=cursor</property> <property name="connection.username">test</property> <property name="connection.password">test</property> <!-- <property name="show_sql">true</property> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test1?characterEncoding=GBK&useUnicode=true</property> <property name="connection.username">root</property> <property name="connection.password" /> --> <mapping resource="schema/person.hbm.xml"/> </session-factory></hibernate-configuration> <property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>改为 http://blog.csdn.net/vvfish119/archive/2005/12/05/543796.aspx不知道对你有没有帮助 谢谢上楼的文章,不过文章里的内容和我的问题不一样我想问题的关键在这里 Could not synchronize database state with session大家知道这句话到底是怎么回事吗 你的配置文件里面应该要使用inverse吧,还有你为什么把动态修改给关了。 搂主还是先好好看看Hibernate关于一对一的文档吧,或者是书籍也行,inverse就在hbm.xml里配置,用于设置主控方……你去看看文档再作例子比较好吧 Could not synchronize database state with session- afterTransactionCompletion() was never called- unclosed connection, forgot to call close() on your session? 好象是执行这个前有session没有关闭 有可视化查看项目结构的工具么? struts2配置问题 hibernate search 对英文进行分词 java 入门 客户端发送请求,从oracle查出数据导出txt格式提示下载 如何知道邮件是否发送成功? EJB客户端代码:改为servlet就报错Cannot instantiate class: org.jnp.interfaces.NamingContextFactory RMI问题,请各位帮忙! 如何结贴?我曾在这个版发文,现在找不到了也不知道如何结贴 网站监控软件资料,谁有,谢谢! 求dreamweaver支持struts的插件 请教关于hibernate3的2级缓存~
以下是我的代码:Person.javapackage model;import java.io.Serializable;
public class Person implements Serializable {
String id;
String name;
int age;
Passport passport;
TGroup group; public Person(){
id="1";
name="";
age=0;
}
public Passport getPassport() {
return passport;
} public void setPassport(Passport passport) {
this.passport = passport;
} public int getAge() {
return age;
} public TGroup getGroup() {
return group;
} public void setGroup(TGroup group) {
this.group = group;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
} public void setId(String id) {
this.id = id;
}}
Passport.javapackage model;import java.io.Serializable;public class Passport implements Serializable{
String id;
String destination;
Person person;
public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} public Passport() {
}
public String getDestination() {
return destination;
} public void setDestination(String destination) {
this.destination = destination;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
}}映射文件:person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping package="model">
<class name="Person" table="person" dynamic-update="false">
<id name="id" column="id" type="string" unsaved-value="null">
<generator class="uuid.hex" />
</id>
<property name="name" type="string" column="name" />
<property name="age" type="int" column="age" />
<one-to-one name="passport" class="Passport" cascade="all" outer-join="true" />
<!-- many-to-one name="group" class="TGroup" column="group_id" unique="true"/-->
</class> <class name="Passport" table="passport" dynamic-update="false">
<id name="id" column="id" type="string" unsaved-value="null">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="destination" type="string" column="destination" />
<one-to-one name="person" class="Person" constrained="true" />
</class>
<class name="TGroup" table="tgroup">
<id name="id" column="id" type="int">
<generator class="native"/>
</id>
<property name="name" type="string" column="name"/>
<!-- one-to-one name="person" class="Person" property-ref="group"/-->
</class></hibernate-mapping>
测试程序personBOpackage bo;
import model.TGroup;
import model.Passport;
import model.Person;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class PersonBO {
Configuration cfg=null;
SessionFactory sf=null;
public PersonBO()throws Exception{
cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
}
public void test() throws HibernateException{
Person person=new Person();
person.setId("9");
person.setAge(25);
person.setName("gyt_6");
Passport ps=new Passport();
ps.setDestination("England");
person.setPassport(ps);
ps.setPerson(person);
Session session=sf.openSession();
Transaction ts=session.beginTransaction();
session.save(person);
ts.commit();
session.flush();
session.close();
}
public static void main(String[] args) {
try{
PersonBO personBO=new PersonBO();
personBO.test();
}
catch(Exception e){
e.printStackTrace();
}
}}
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="jdbc.fetch_size">20</property>
<property name="jdbc.batch_size">30</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test;SelectMethod=cursor</property>
<property name="connection.username">test</property>
<property name="connection.password">test</property>
<!--
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test1?characterEncoding=GBK&useUnicode=true</property>
<property name="connection.username">root</property>
<property name="connection.password" /> --> <mapping resource="schema/person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
改为
我想问题的关键在这里
Could not synchronize database state with session大家知道这句话到底是怎么回事吗
- afterTransactionCompletion() was never called
- unclosed connection, forgot to call close() on your session?
好象是执行这个前有session没有关闭