Order order = (Order) session.get(Order.class, 1); System.out.println(order.getOrderName() + ": "+order.getOrderId()); //这里值用到了order对象的属性,没有用的customer的属性,所以不会发送取查询customer相关的sql语句的。 }
1 2 3 4 5 6 7 8 9 10 11
Hibernate: //通过这个hibernate的sql输出我们看到没有去查询customer相关的。 select order0_.hb_order_id as hb1_3_0_, order0_.hb_order_name as hb2_3_0_, order0_.customer_id as customer3_3_0_ from mage.hb_order order0_ where order0_.hb_order_id=? order 1: 1
Hibernate: select order0_.hb_order_id as hb1_3_0_, order0_.hb_order_name as hb2_3_0_, order0_.customer_id as customer3_3_0_ from mage.hb_order order0_ where order0_.hb_order_id=? Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=?
Hibernate: select order0_.hb_order_id as hb1_3_0_, order0_.hb_order_name as hb2_3_0_, order0_.customer_id as customer3_3_0_ from mage.hb_order order0_ where order0_.hb_order_id=? order 1: 1 =destroy=
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) at com.mamh.hibernate.demo.entities.Customer_$$_javassist_1.toString(Customer_$$_javassist_1.java) at java.lang.String.valueOf(String.java:2994) at java.io.PrintStream.println(PrintStream.java:821) at com.mamh.hibernate.demo.HibernateTest.testManyToOneGet(HibernateTest.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
获取order对象时,默认情况下,其关联的customer对象是一个代理对象!
3.测试更新order,customer对象
1 2 3 4 5
@Test public void testManyToOneUpdate() { Order order = (Order) session.get(Order.class, 1); order.getCustomer().setCustomerName("new customer name"); }
Hibernate: select order0_.hb_order_id as hb1_3_0_, order0_.hb_order_name as hb2_3_0_, order0_.customer_id as customer3_3_0_ from mage.hb_order order0_ where order0_.hb_order_id=? Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=? =destroy= Hibernate: update mage.hb_customer set hb_customer_name=? where hb_customer_id=?
Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=? =destroy= Nov 17, 2017 2:51:23 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 1451, SQLState: 23000 Hibernate: delete from mage.hb_customer where hb_customer_id=? Nov 17, 2017 2:51:23 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Cannot delete or update a parent row: a foreign key constraint fails (`mage`.`hb_order`, CONSTRAINT `FK1C0AD3C966B2F7AA` FOREIGN KEY (`customer_id`) REFERENCES `hb_customer` (`hb_customer_id`)) Nov 17, 2017 2:51:23 PM org.hibernate.engine.jdbc.batch.internal.BatchingBatch performExecution ERROR: HHH000315: Exception executing batch [Cannot delete or update a parent row: a foreign key constraint fails (`mage`.`hb_order`, CONSTRAINT `FK1C0AD3C966B2F7AA` FOREIGN KEY (`customer_id`) REFERENCES `hb_customer` (`hb_customer_id`))]
org.hibernate.exception.ConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`mage`.`hb_order`, CONSTRAINT `FK1C0AD3C966B2F7AA` FOREIGN KEY (`customer_id`) REFERENCES `hb_customer` (`hb_customer_id`))
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:128) 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.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129) at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) at com.sun.proxy.$Proxy9.executeBatch(Unknown Source) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:110) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.doExecuteBatch(BatchingBatch.java:101) at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.execute(AbstractBatchImpl.java:161) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.executeBatch(JdbcCoordinatorImpl.java:162) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:357) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:280) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1127) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:325) 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.mamh.hibernate.demo.HibernateTest.destroy(HibernateTest.java:315) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`mage`.`hb_order`, CONSTRAINT `FK1C0AD3C966B2F7AA` FOREIGN KEY (`customer_id`) REFERENCES `hb_customer` (`hb_customer_id`)) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.cj.core.util.Util.handleNewInstance(Util.java:185) at com.mysql.cj.core.util.Util.getInstance(Util.java:168) at com.mysql.cj.core.util.Util.getInstance(Util.java:175) at com.mysql.cj.jdbc.exceptions.SQLError.createBatchUpdateException(SQLError.java:636) at com.mysql.cj.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1734) at com.mysql.cj.jdbc.PreparedStatement.executeBatchInternal(PreparedStatement.java:1217) at com.mysql.cj.jdbc.StatementImpl.executeBatch(StatementImpl.java:1009) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122) ... 37 more Caused by: java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`mage`.`hb_order`, CONSTRAINT `FK1C0AD3C966B2F7AA` FOREIGN KEY (`customer_id`) REFERENCES `hb_customer` (`hb_customer_id`)) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:533) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115) at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983) at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1826) at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2034) at com.mysql.cj.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1712) ... 45 more