您的位置:首页 > 其它

一个通用的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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: