Given the following code: {code} @Entity @IdClass(EmployeeEntityId.class) class EmployeeEntity {
@Id Long id;
@Id String name;
Integer salary;
public EmployeeEntity() { }
public EmployeeEntity(Long id, String name) { this.id = id; this.name = name; }
public Integer getSalary() { return salary; }
public void setSalary(Integer salary) { this.salary = salary; } }
@Entity @IdClass(EmployeeEntityId.class) class EmployeeInfo {
@Id @OneToOne @JoinColumns({ @JoinColumn(name="id_ref", referencedColumnName = "id"), @JoinColumn(name="name_ref", referencedColumnName = "name")
}) EmployeeEntity employee;
public EmployeeInfo() { }
public EmployeeInfo(EmployeeEntity employee) { this.employee = employee; } }
/** * Created by Pawel_Solarski on 7/20/2016. */ class EmployeeEntityId implements Serializable {
private Long id; private String name;
public EmployeeEntityId() { }
public EmployeeEntityId(Long id, String name) { this.id = id; this.name = name; }
public Long getId() { return id; }
public String getName() { return name; } } {code}
When executing the following code: {code} public class DerivedIdExample extends JPARunner {
public static void main(String[] args) { DerivedIdExample example = new DerivedIdExample(); example.withDBOpenAndClosed(() -> { example.withTransaction(em -> { EmployeeEntity employee = new EmployeeEntity(1L, "Pawel"); EmployeeInfo info = new EmployeeInfo(employee); em.persist(employee); em.persist(info); }); }); } } {code}
the exception is raised: {code} Jul 20, 2016 3:28:11 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation INFO: HHH000204: Processing PersistenceUnitInfo [ name: mysql_hbm ...] Jul 20, 2016 3:28:12 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.1.Final} Jul 20, 2016 3:28:12 PM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found Jul 20, 2016 3:28:12 PM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist Jul 20, 2016 3:28:12 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Jul 20, 2016 3:28:12 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) Jul 20, 2016 3:28:12 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test] Jul 20, 2016 3:28:12 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} Jul 20, 2016 3:28:12 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false Jul 20, 2016 3:28:12 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) Wed Jul 20 15:28:12 CEST 2016 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. Jul 20, 2016 3:28:12 PM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect Jul 20, 2016 3:28:13 PM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect Jul 20, 2016 3:28:13 PM org.hibernate.mapping.RootClass checkCompositeIdentifier WARN: HHH000038: Composite-id class does not override equals(): jpa.advanced.EmployeeEntityId Jul 20, 2016 3:28:13 PM org.hibernate.mapping.RootClass checkCompositeIdentifier WARN: HHH000039: Composite-id class does not override hashCode(): jpa.advanced.EmployeeEntityId Jul 20, 2016 3:28:13 PM org.hibernate.mapping.RootClass checkCompositeIdentifier WARN: HHH000038: Composite-id class does not override equals(): jpa.advanced.EmployeeInfo Jul 20, 2016 3:28:13 PM org.hibernate.mapping.RootClass checkCompositeIdentifier WARN: HHH000039: Composite-id class does not override hashCode(): jpa.advanced.EmployeeInfo Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: mysql_hbm] Unable to build Hibernate SessionFactory at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:961) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:891) at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) at jpa.JPARunner.<init>(JPARunner.java:18) at jpa.advanced.DerivedIdExample.<init>(DerivedIdExample.java:11) at jpa.advanced.DerivedIdExample.main(DerivedIdExample.java:14) 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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: org.hibernate.MappingException: Composite-id class must implement Serializable: jpa.advanced.EmployeeInfo at org.hibernate.mapping.RootClass.checkCompositeIdentifier(RootClass.java:291) at org.hibernate.mapping.RootClass.validate(RootClass.java:274) at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:482) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) ... 11 more {code}
and when class EmployeeInfo implements Serializable, the problem is gone.
Based on the JSR 338, chapter 2.4.1.3 examples, the dependent class is not serializable in any case and does not seen to be spec requirement.
The other problem is that the message *Composite-id class must implement Serializable: jpa.advanced.EmployeeInfo* is misleading, because EmployeeInfo is not composite class, and real composite-id class ( EmployeeId EmployeeEntityId .class) implements Serializable.
|
|