·代码如下:
# MySQL-Front 3.2 (Build 14.3)
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES latin1 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='SYSTEM' */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */;
/*!40101 SET SQL_MODE='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES */;
/*!40103 SET SQL_NOTES='ON' */;
# Host: localhost Database: test
# ------------------------------------------------------
# Server version 5.0.26-community-nt
#
# Table structure for table customer
#
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer` (
`ID` bigint(20) NOT NULL auto_increment,
`NAME` varchar(40) NOT NULL,
`PASSWORD` varchar(40) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
#
# Dumping data for table customer
#
INSERT INTO `customer` VALUES (1,'HUDA','HUDA');
INSERT INTO `customer` VALUES (2,'LIULINLIN','LIULINLIN');
INSERT INTO `customer` VALUES (3,'HUANGWEIBIAO','HUANGWEIBIAO');
INSERT INTO `customer` VALUES (4,'WANGJIN','WANGJIN');
INSERT INTO `customer` VALUES (5,'ZOUJIA','ZOUJIA');
INSERT INTO `customer` VALUES (6,'liuwei','liuwei');
#
# Table structure for table orders
#
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`ID` bigint(20) NOT NULL auto_increment,
`ORDERNAME` varchar(45) NOT NULL,
`CUSTOMER_ID` bigint(20) NOT NULL,
PRIMARY KEY (`ID`),
KEY `CUSTOMER_ID` (`CUSTOMER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
#
# Dumping data for table orders
#
INSERT INTO `orders` VALUES (1,'CLOTHES',2);
INSERT INTO `orders` VALUES (2,'PSP',1);
INSERT INTO `orders` VALUES (3,'GIRLFRIEND',3);
INSERT INTO `orders` VALUES (4,'TV',4);
INSERT INTO `orders` VALUES (5,'HOUSE',5);
INSERT INTO `orders` VALUES (6,'PS2',1);
#
# Foreign keys for table orders
#
ALTER TABLE `orders`
ADD FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer` (`ID`) ON UPDATE CASCADE;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
----------------------------------------------------------------------------------------
·用myeclipse生成了两个XML映射文件和两个POJO文件;
Customer.hbm.xml代码入下:
<?xml version="1.0"?>
<!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 - Hibernate Tools
-->
<hibernate-mapping>
<class name="test.Hibernate.Customer" table="customer" catalog="test">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="40" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="40" not-null="true" />
</property>
<set name="orderses" inverse="false" cascade="none" >
<key>
<column name="CUSTOMER_ID" not-null="true" />
</key>
<one-to-many class="test.Hibernate.Orders" />
</set>
</class>
</hibernate-mapping>
------------------------------------------------------------------------------------
Orders.hbm.xml代码如下:
<?xml version="1.0"?>
<!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 - Hibernate Tools
-->
<hibernate-mapping>
<class name="test.Hibernate.Orders" table="orders" catalog="test">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="customer" class="test.Hibernate.Customer" fetch="select">
<column name="CUSTOMER_ID" not-null="true" />
</many-to-one>
<property name="ordername" type="java.lang.String">
<column name="ORDERNAME" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------------
级联删除代码如下:
package test.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;
import org.hibernate.classic.Session;
public class EditClass {
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer(new Long(1),"huda","huda");
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
}
}
------------------------------------------------------------------------------------
报错如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
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:141)
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 test.Hibernate.EditClass.main(EditClass.java:18)
Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'CUSTOMER_ID' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:657)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 8 more
======================================================================================
我的思想是,只对主控表CUSTOMER中的一条记录删除.那么hibernate会自动帮我级联删除ORDERS表中的相关客户记录.不知道对不对
一个朋友告诉我说,要级联删除,他的方法是执行两次删除,先删定单表客户记录再删客户表记录.
不知道对不对。也就是先执行外键删除,再执行主控表中的主键删除.
希望高人指点,刚开始涉猎HIBERNATE,希望能帮我越过这个障碍,谢谢了
6 个解决方案
#1
<set name="orderses" inverse="false" cascade="all" >
<key>
<column name="CUSTOMER_ID" not-null="true" />
</key>
<one-to-many class="test.Hibernate.Orders" />
</set>
<key>
<column name="CUSTOMER_ID" not-null="true" />
</key>
<one-to-many class="test.Hibernate.Orders" />
</set>
#2
这位大哥,cascade是all也不对.当然,这里必须是要改的.
还是报错!!
还是报错!!
#3
关键报错如下:
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer(new Long(6),"liuwei","liuwei");
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer(new Long(6),"liuwei","liuwei");
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
#4
上边的粘错了
Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'CUSTOMER_ID' at row 1
Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'CUSTOMER_ID' at row 1
#5
倒,你自己看
Customer c = new Customer(new Long(6),"liuwei","liuwei");
session.delete(c);
你new一个对象,但并没有保存进数据库,你再删除c?请问,你删除什么?
Customer c = new Customer(new Long(6),"liuwei","liuwei");
session.delete(c);
你new一个对象,但并没有保存进数据库,你再删除c?请问,你删除什么?
#6
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = (Customer)session.load(Customer.class,new Long(1));
System.out.println("要删除对象的主键是: " + c.getId());
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
这样写的话还是错的.报一样的异常啊.而且数据库都不为空的
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = (Customer)session.load(Customer.class,new Long(1));
System.out.println("要删除对象的主键是: " + c.getId());
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
这样写的话还是错的.报一样的异常啊.而且数据库都不为空的
#1
<set name="orderses" inverse="false" cascade="all" >
<key>
<column name="CUSTOMER_ID" not-null="true" />
</key>
<one-to-many class="test.Hibernate.Orders" />
</set>
<key>
<column name="CUSTOMER_ID" not-null="true" />
</key>
<one-to-many class="test.Hibernate.Orders" />
</set>
#2
这位大哥,cascade是all也不对.当然,这里必须是要改的.
还是报错!!
还是报错!!
#3
关键报错如下:
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer(new Long(6),"liuwei","liuwei");
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer(new Long(6),"liuwei","liuwei");
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
#4
上边的粘错了
Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'CUSTOMER_ID' at row 1
Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'CUSTOMER_ID' at row 1
#5
倒,你自己看
Customer c = new Customer(new Long(6),"liuwei","liuwei");
session.delete(c);
你new一个对象,但并没有保存进数据库,你再删除c?请问,你删除什么?
Customer c = new Customer(new Long(6),"liuwei","liuwei");
session.delete(c);
你new一个对象,但并没有保存进数据库,你再删除c?请问,你删除什么?
#6
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = (Customer)session.load(Customer.class,new Long(1));
System.out.println("要删除对象的主键是: " + c.getId());
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
这样写的话还是错的.报一样的异常啊.而且数据库都不为空的
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer c = (Customer)session.load(Customer.class,new Long(1));
System.out.println("要删除对象的主键是: " + c.getId());
session.delete(c);
tx.commit();
session.close();
System.out.println(c+"_______________1");
这样写的话还是错的.报一样的异常啊.而且数据库都不为空的