一个通用的Hibernate DAO
2009-11-19 16:54
375 查看
经过one-to-one和one-to-many测试没有问题,看直接复制到任何需要DAO的工程中使用
代码
强烈建议在实际使用中加个接口
BaseDAO.JAVA
package com.lusm.HibernateSessionFactory;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
Exception in thread "main" org.hibernate.TransientObjectException: the detached instance passed to delete() had a null identifier
.........或者
Exception in thread "main" org.hibernate.TransientObjectException: the detached instance passed to delete() had a null identifier
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:63)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:761)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:739)
at com.lusm.HibernateSessionFactory.BaseDAO.delete(BaseDAO.java:44)
at com.lusm.main.Del.main(Del.java:19)或
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.lusm.HibernateSessionFactory.BaseDAO.delete(BaseDAO.java:46)
at com.lusm.main.Del.main(Del.java:18)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`lusm/test1`, CONSTRAINT `test1_ibfk_1` FOREIGN KEY (`id`) REFERENCES `test` (`id`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
<?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="com.lusm.test.Test1" table="test1" catalog="lusm">
<id name="sid" type="java.lang.Integer">
<column name="sid" />
<generator class="increment" />
</id>
<many-to-one name="test" class="com.lusm.test.Test" fetch="select" cascade="save-update" >
<column name="id" not-null="true" />
</many-to-one>
<property name="sname" type="java.lang.String">
<column name="sname" length="20" />
</property>
</class>
</hibernate-mapping>
code
package com.lusm.test;
<?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="com.lusm.test.Test" table="test" catalog="lusm">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<set name="test1s" inverse="true">
<key>
<column name="id" not-null="true" />
</key>
<one-to-many class="com.lusm.test.Test1"/>
</set>
</class>
</hibernate-mapping>
code
package com.lusm.test;
import java.util.HashSet;
import java.util.Set;
create table `lusm`.`test1`(
`sid` INT not null auto_increment,
`id` INT not null,
`sname` varchar(20),
primary key (`sid`),
index(sid),
foreign key(id) references test(id) ON DELETE CASCADE ON UPDATE CASCADE
);
create table `lusm`.`test`(
`id` INT not null auto_increment,
`name` VARCHAR(20),
primary key (`id`)
);
下面给出 该示例的两个测试类
insert
package com.lusm.main;
import com.lusm.HibernateSessionFactory.BaseDAO;
import com.lusm.test.Test;
import com.lusm.test.Test1;
[/b]package com.lusm.main;
import com.lusm.HibernateSessionFactory.BaseDAO;
import com.lusm.test.Test;
public class Del
/** *//**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception Test test = new Test();
test.setId(1);
BaseDAO bd = new BaseDAO();
bd.delete(test);
}
}
代码
强烈建议在实际使用中加个接口
BaseDAO.JAVA
package com.lusm.HibernateSessionFactory;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
Exception in thread "main" org.hibernate.TransientObjectException: the detached instance passed to delete() had a null identifier
.........或者
Exception in thread "main" org.hibernate.TransientObjectException: the detached instance passed to delete() had a null identifier
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:63)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:761)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:739)
at com.lusm.HibernateSessionFactory.BaseDAO.delete(BaseDAO.java:44)
at com.lusm.main.Del.main(Del.java:19)或
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.lusm.HibernateSessionFactory.BaseDAO.delete(BaseDAO.java:46)
at com.lusm.main.Del.main(Del.java:18)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`lusm/test1`, CONSTRAINT `test1_ibfk_1` FOREIGN KEY (`id`) REFERENCES `test` (`id`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
<?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="com.lusm.test.Test1" table="test1" catalog="lusm">
<id name="sid" type="java.lang.Integer">
<column name="sid" />
<generator class="increment" />
</id>
<many-to-one name="test" class="com.lusm.test.Test" fetch="select" cascade="save-update" >
<column name="id" not-null="true" />
</many-to-one>
<property name="sname" type="java.lang.String">
<column name="sname" length="20" />
</property>
</class>
</hibernate-mapping>
code
package com.lusm.test;
<?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="com.lusm.test.Test" table="test" catalog="lusm">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<set name="test1s" inverse="true">
<key>
<column name="id" not-null="true" />
</key>
<one-to-many class="com.lusm.test.Test1"/>
</set>
</class>
</hibernate-mapping>
code
package com.lusm.test;
import java.util.HashSet;
import java.util.Set;
create table `lusm`.`test1`(
`sid` INT not null auto_increment,
`id` INT not null,
`sname` varchar(20),
primary key (`sid`),
index(sid),
foreign key(id) references test(id) ON DELETE CASCADE ON UPDATE CASCADE
);
create table `lusm`.`test`(
`id` INT not null auto_increment,
`name` VARCHAR(20),
primary key (`id`)
);
下面给出 该示例的两个测试类
insert
package com.lusm.main;
import com.lusm.HibernateSessionFactory.BaseDAO;
import com.lusm.test.Test;
import com.lusm.test.Test1;
[/b]package com.lusm.main;
import com.lusm.HibernateSessionFactory.BaseDAO;
import com.lusm.test.Test;
public class Del
/** *//**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception Test test = new Test();
test.setId(1);
BaseDAO bd = new BaseDAO();
bd.delete(test);
}
}
相关文章推荐
- 一个通用的泛型GenericHibernateDAO
- 一个通用的Hibernate DAO
- 一个通用的泛型GenericHibernateDao
- 一个通用的泛型 GenericHibernateDao
- 一个通用的Hibernate DAO
- 一个通用的泛型GenericHibernateDao
- 一个通用的DAO接口及其Hibernate3实现
- 基于Spring、Hibernate的通用DAO层与Service层的实现
- Hibernate 通用底层Dao
- Cody_Yu 通用的DAO查询,常见方法集合,基于Oracle&Hibernate&Spring,集合了多种常用方法,方便调用。
- 基于Spring、Hibernate的通用DAO层与Service层的实现
- JAVA通用Dao接口和hibernate的实现
- 基于Spring、Hibernate的通用DAO层与Service层的实现
- 一个泛型Hibernate DAO实现
- 利用反射泛型编写一个通用Dao
- 后端框架中,如Hibernate框架,为什么老是喜欢在XXdao.java的类中设置一个static成员变量
- Hibernate实现通用DAO
- 基于泛型的通用Dao接口和hibernate的实现
- 基于Spring、Hibernate的通用DAO层与Service层的实现
- 在Hibernate中的一个对tree树节点查询封装的通用工具类