自己理解:
单向1对多。 一个客户可以发出多个订单。但是一个订单只属于一个客户。 写对象的时候。在多的那个类对象把1的作为自己的一个属性。 写配置文件 <many-to-one name=1的属性名。class=1的类名。column=1的主键名>
多的1方的Java类。 把1的作为一个属性放到多的里面。
package com.hibernate.n21; public class Order { private Integer orderId; private String orderName; private Customer customer; private Integer getOrderId() { return orderId; } public void setOrderId(Integer orderId) { this.orderId = orderId; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public Order(String orderName, Customer customer) { super(); this.orderName = orderName; this.customer = customer; } }
映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hibernate.n21"> <class name="Order" table="orders" > <id name="orderId" type="java.lang.Integer"> <column name="order_Id" /> <!-- 指定主键的生成方式, native: 使用数据库本地方式 --> <generator class="native" /> </id> <property name="orderName" type="java.lang.String" column="order_Name" > </property> <!-- 映射n-1的关联关系
name:1的属性名。把1当成多的一个属性。
class 1的类名。
column 1的主键名。
--> <many-to-one name="customer" class="Customer" column="customer_Id"></many-to-one> </class> </hibernate-mapping>
1的类名:
package com.hibernate.n21; public class Customer { private Integer customerId; private String customerName; public Integer getCustomerId() { return customerId; } public void setCustomerId(Integer customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public Customer() { super(); } public Customer(String customerName) { super(); this.customerName = customerName; } }
映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hibernate.n21"> <class name="Customer" table="customer" > <id name="customerId" type="java.lang.Integer"> <column name="customer_Id" /> <!-- 指定主键的生成方式, native: 使用数据库本地方式 --> <generator class="native" /> </id> <property name="customerName" type="java.lang.String" column="customer_Name" > </property> </class> </hibernate-mapping>
然后在主的映射文件hibernate.cfg.xml文件中加上映射
<mapping resource="com/hibernate/n21/Customer.hbm.xml"/> <mapping resource="com/hibernate/n21/Order.hbm.xml"/>
SAVE():
1 先保存1的一端 只是三条insert语句。 2 @org.junit.Test 3 public void testN21Save(){ 4 Customer customer=new Customer(); 5 customer.setCustomerName("C1"); 6 Order order1 =new Order(); 7 order1.setOrderName("O1"); 8 Order order2 =new Order(); 9 order2.setOrderName("O2"); 10 11 //设置关联关系。就是给多的一方设置外键 12 order1.setCustomer(customer); 13 order2.setCustomer(customer); 14 session.save(customer); 15 session.save(order1); 16 session.save(order2); 17 18 }
1 先保存多的一端。3条insert语句。2条update语句
因为在插入多的一端时,无法确定1的一端的外键值。所以只能先为null。等1的一端插入后。再进行修改。 2 @org.junit.Test 3 public void testN21Save(){ 4 Customer customer=new Customer(); 5 customer.setCustomerName("C1"); 6 Order order1 =new Order(); 7 order1.setOrderName("O1"); 8 Order order2 =new Order(); 9 order2.setOrderName("O2"); 10 11 //设置关联关系。就是给多的一方设置外键 12 order1.setCustomer(customer); 13 order2.setCustomer(customer); 14 15 session.save(order1); 16 session.save(order2); 17 session.save(customer); 18 }
select()方法:
如果在第5行关闭session。就会出现懒加载异常。因为System.out.println(order.getCustomer().getClass()); class com.hibernate.n21.Customer_$$_javassist_0
得到的是一个customer代理对象。
1 @org.junit.Test 2 public void testN21Select(){ 3 Order order=(Order) session.get(Order.class, 5); 4 System.out.println(order.getOrderName()); 5 session.close(); 6 Customer customer = order.getCustomer(); 7 System.out.println(customer.getCustomerName()); 8 }
对于懒加载异常。我们可以在hibernate.cfg.xml中设置立即加载。
<class name="Customer" table="customer" lazy="false"> 我们设置懒加载为否。就是立即加载
Update():
@org.junit.Test public void testN21Update(){ Order order=(Order) session.get(Order.class, 5); Customer customer=order.getCustomer(); customer.setCustomerName("N21Update"); }
Delete:
//删除多的,直接删除
@org.junit.Test public void testN21Delete(){ Order order=(Order) session.get(Order.class, 5); session.delete(order); }
//不设置级联删除的话。删除1的一方。会出现异常。因为1的一方。对应好几个
//但是1的一方没有被引用的话。是可以删除的
1 @org.junit.Test 2 public void testN21Delete(){ 3 Customer customer=(Customer) session.get(Customer.class, 3); 4 session.delete(customer); 5 }
org.hibernate.exception.ConstraintViolationException: could not execute statement at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136) at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3343) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3546) at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:100) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:293) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) at com.hibernate.n21.Test.destroy(Test.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`luwei`.`orders`, CONSTRAINT `FK_qaw4j85b8ppgne3sg6f218msv` FOREIGN KEY (`customer_Id`) REFERENCES `customer` (`customer_Id`)) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133) ... 37 more