本文主要是介绍8.1.1 Hibernate:一对一单向关联(unidirectional),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
将 @OneToOne
放在表 phone_detail
的映射类中,因为外键定义在 phone_detail
表中。
1 定义映射类
1.1 表 phone
的映射类定义:
package hibernate;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "phone")
public class Phone {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")private int id;@Column(name = "imei")private String imei;@Column(name = "number")private String number;public Phone() {}public Phone(String imei, String number) {this.imei = imei;this.number = number;}// 省略 Getters 和 Setters ...}
1.2 表 phone_detail
的映射类定义:
package hibernate;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;@Entity
@Table(name = "phone_detail")
public class PhoneDetail {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")private int id;@OneToOne(cascade = CascadeType.ALL)@JoinColumn(name = "phone_id")private Phone phone;@Column(name = "manufacturer")private String manufacturer;@Column(name = "model")private String model;public PhoneDetail() {}public PhoneDetail(String manufacturer, String model) {this.manufacturer = manufacturer;this.model = model;}// 省略 Getters 和 Setters ...}
2 测试
2.1 新增
2.1.1 同时新建两个对象并建立引用关系,插入 Phone
对象
package hibernate;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;public class HibernateTest {@Testpublic void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();Phone phone = new Phone("812345678912345", "18012345678");PhoneDetail phoneDetail = new PhoneDetail("XXX", "XXX-2017");phoneDetail.setPhone(phone);session.save(phone);transaction.commit();session.close();sessionFactory.close();}}
运行测试,从日志中可以看出,实际上 Hibernate 只向 phone
表中插入了一条数据,并未向 phone_detail
表中插入任何数据。
七月 06, 2017 11:41:50 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
七月 06, 2017 11:41:50 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 06, 2017 11:41:51 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
七月 06, 2017 11:41:51 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
七月 06, 2017 11:41:51 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]
七月 06, 2017 11:41:51 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
七月 06, 2017 11:41:51 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
七月 06, 2017 11:41:51 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 2 (min=1)
Thu Jul 06 23:41:52 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
七月 06, 2017 11:41:52 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
七月 06, 2017 11:41:52 下午 org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
Hibernate: insert intophone(imei, number) values(?, ?)
七月 06, 2017 11:41:54 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/test]
2.1.2 同时新建两个对象并建立引用关系,插入 PhoneDetail
对象
将单元测试中 session.save(phone);
替换成 session.save(phoneDetail);
运行测试,从日志中可以看出,Hibernate 不仅向 phone_detail
表中插入了一条数据,还向 phone
表中插入关联数据,由此可见 PhoneDetail
类维护级联关系。
七月 06, 2017 11:48:03 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
七月 06, 2017 11:48:03 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 06, 2017 11:48:04 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
七月 06, 2017 11:48:04 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
七月 06, 2017 11:48:04 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]
七月 06, 2017 11:48:04 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
七月 06, 2017 11:48:04 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
七月 06, 2017 11:48:04 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 2 (min=1)
Thu Jul 06 23:48:04 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
七月 06, 2017 11:48:05 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
七月 06, 2017 11:48:05 下午 org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
Hibernate: insert intophone(imei, number) values(?, ?)
Hibernate: insert intophone_detail(manufacturer, model, phone_id) values(?, ?, ?)
七月 06, 2017 11:48:07 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/test]
注意:@OneToOne
的 cascade
属性一定要设置,否则会出现以下异常导致两条数据全部插入失败!@OneToOne
的 cascade
属性后续将详细阐述。
org.hibernate.exception.ConstraintViolationException: could not execute statementat org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2909)at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3480)at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:626)at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:280)at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:261)at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:306)at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318)at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275)at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:689)at org.hibernate.internal.SessionImpl.save(SessionImpl.java:681)at org.hibernate.internal.SessionImpl.save(SessionImpl.java:676)at hibernate.HibernateTest.test(HibernateTest.java:20)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.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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'phone_id' cannot be nullat 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.jdbc.Util.handleNewInstance(Util.java:425)at com.mysql.jdbc.Util.getInstance(Util.java:408)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:205)... 45 more
2.2 删除
基于以上测试新增的数据进行删除测试。
2.2.1 修改单元测试代码,删除 Phone
@Test
public void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();Phone phone = (Phone) session.createQuery(" FROM Phone").list().get(0);session.delete(phone);transaction.commit();session.close();sessionFactory.close();
}
运行测试,出现违反约束异常:
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statementat org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1441)at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:491)at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3201)at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2411)at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)at hibernate.HibernateTest.test(HibernateTest.java:19)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.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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statementat org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3315)at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3552)at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:99)at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589)at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1435)... 32 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`phone_detail`, CONSTRAINT `FK_PHONE_DETAIL` FOREIGN KEY (`phone_id`) REFERENCES `phone` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)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.jdbc.Util.handleNewInstance(Util.java:425)at com.mysql.jdbc.Util.getInstance(Util.java:408)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:205)... 41 more
2.2.2 修改单元测试代码,删除 PhoneDetail
@Test
public void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();PhoneDetail phoneDetail = (PhoneDetail) session.createQuery(" FROM PhoneDetail").list().get(0);session.delete(phoneDetail);transaction.commit();session.close();sessionFactory.close();
}
运行测试,从日志中看出只删除了 PhoneDetail
对象映射的 phone_detail
表中记录,其关联的 phone
表中记录未被删除。
七月 07, 2017 10:54:19 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
七月 07, 2017 10:54:19 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 07, 2017 10:54:20 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
七月 07, 2017 10:54:20 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
七月 07, 2017 10:54:20 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]
七月 07, 2017 10:54:20 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
七月 07, 2017 10:54:20 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
七月 07, 2017 10:54:20 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 2 (min=1)
Fri Jul 07 22:54:20 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
七月 07, 2017 10:54:21 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
七月 07, 2017 10:54:21 下午 org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
七月 07, 2017 10:54:23 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: selectphonedetai0_.id as id1_1_,phonedetai0_.manufacturer as manufact2_1_,phonedetai0_.model as model3_1_,phonedetai0_.phone_id as phone_id4_1_ fromphone_detail phonedetai0_
Hibernate: selectphone0_.id as id1_0_0_,phone0_.imei as imei2_0_0_,phone0_.number as number3_0_0_ fromphone phone0_ wherephone0_.id=?
Hibernate: delete fromphone_detail whereid=?
七月 07, 2017 10:54:23 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/test]
2.3 更新
基于新增测试的数据进行更新测试。
2.3.1 同时修改持久化的 Phone
和 PhoneDetail
对象,更新 Phone
@Test
public void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();PhoneDetail phoneDetail = (PhoneDetail) session.createQuery(" FROM PhoneDetail").list().get(0);phoneDetail.setManufacturer("YYY");phoneDetail.setModel("YYY-2017");Phone phone = phoneDetail.getPhone();phone.setImei("987654321987654");phone.setNumber("13987654321");session.update(phone);transaction.commit();session.close();sessionFactory.close();
}
运行测试,从日志中可以看出两张表中记录级联更新了。
七月 07, 2017 11:32:32 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
七月 07, 2017 11:32:32 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 07, 2017 11:32:33 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
七月 07, 2017 11:32:33 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
七月 07, 2017 11:32:33 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]
七月 07, 2017 11:32:33 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
七月 07, 2017 11:32:33 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
七月 07, 2017 11:32:33 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 2 (min=1)
Fri Jul 07 23:32:34 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
七月 07, 2017 11:32:34 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
七月 07, 2017 11:32:34 下午 org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
七月 07, 2017 11:32:36 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: selectphonedetai0_.id as id1_1_,phonedetai0_.manufacturer as manufact2_1_,phonedetai0_.model as model3_1_,phonedetai0_.phone_id as phone_id4_1_ fromphone_detail phonedetai0_
Hibernate: selectphone0_.id as id1_0_0_,phone0_.imei as imei2_0_0_,phone0_.number as number3_0_0_ fromphone phone0_ wherephone0_.id=?
Hibernate: updatephone_detail setmanufacturer=?,model=?,phone_id=? whereid=?
Hibernate: updatephone setimei=?,number=? whereid=?
七月 07, 2017 11:32:37 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/test]
2.3.2 同时修改持久化的 Phone
和 PhoneDetail
对象,更新 PhoneDetail
将单元测试中 session.update(phone);
替换成 session.update(phoneDetail);
,运行测试,从日志中可以看出,两张表也都级联更新了。
七月 07, 2017 11:36:55 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
七月 07, 2017 11:36:55 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 07, 2017 11:36:56 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
七月 07, 2017 11:36:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
七月 07, 2017 11:36:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]
七月 07, 2017 11:36:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
七月 07, 2017 11:36:56 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
七月 07, 2017 11:36:56 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 2 (min=1)
Fri Jul 07 23:36:56 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
七月 07, 2017 11:36:57 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
七月 07, 2017 11:36:57 下午 org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
七月 07, 2017 11:36:59 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: selectphonedetai0_.id as id1_1_,phonedetai0_.manufacturer as manufact2_1_,phonedetai0_.model as model3_1_,phonedetai0_.phone_id as phone_id4_1_ fromphone_detail phonedetai0_
Hibernate: selectphone0_.id as id1_0_0_,phone0_.imei as imei2_0_0_,phone0_.number as number3_0_0_ fromphone phone0_ wherephone0_.id=?
Hibernate: updatephone_detail setmanufacturer=?,model=?,phone_id=? whereid=?
Hibernate: updatephone setimei=?,number=? whereid=?
七月 07, 2017 11:36:59 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/test]
2.4 查询
基于新增测试的数据进行查询测试。
@Test
public void test() {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();PhoneDetail phoneDetail = (PhoneDetail) session.createQuery(" FROM PhoneDetail").list().get(0);System.out.println("Phone Detail:");System.out.println(phoneDetail);transaction.commit();session.close();sessionFactory.close();
}
运行测试,日志如下:
七月 07, 2017 11:42:45 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
七月 07, 2017 11:42:45 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 07, 2017 11:42:46 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
七月 07, 2017 11:42:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
七月 07, 2017 11:42:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]
七月 07, 2017 11:42:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
七月 07, 2017 11:42:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
七月 07, 2017 11:42:46 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 2 (min=1)
Fri Jul 07 23:42:46 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
七月 07, 2017 11:42:47 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
七月 07, 2017 11:42:47 下午 org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
七月 07, 2017 11:42:49 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: selectphonedetai0_.id as id1_1_,phonedetai0_.manufacturer as manufact2_1_,phonedetai0_.model as model3_1_,phonedetai0_.phone_id as phone_id4_1_ fromphone_detail phonedetai0_
Hibernate: selectphone0_.id as id1_0_0_,phone0_.imei as imei2_0_0_,phone0_.number as number3_0_0_ fromphone phone0_ wherephone0_.id=?
Phone Detail:
PhoneDetail [id=1, phone=Phone [id=1, imei=111112222233333, number=13387654321], manufacturer=ZZZ, model=ZZZ-2017]
七月 07, 2017 11:42:50 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/test]
这篇关于8.1.1 Hibernate:一对一单向关联(unidirectional)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!