我在使用@Inheritance(strategy = InheritanceType.JOINED)时遇到问题,此问题涉及四个类,分别是Person(超类),Supplier(子类),Client(子类),Transaction(与客户和供应商类)。
现在,我想知道是什么导致了这个小问题: 当我删除供应商类别时,一切正常,
这是堆栈跟踪:
Exception in Application init method
java.lang.reflect.InvocationTargetException
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.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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 sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application init method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:912)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1085)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:137)
at com.abdofx.demo.DemoApplication.bootstrapSpringApplicationContext(DemoApplication.java:48)
at com.abdofx.demo.DemoApplication.init(DemoApplication.java:25)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841)
... 2 more
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:970)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:895)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:388)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706)
... 18 more
Caused by: org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.JoinedSubclassEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:112)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:128)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:300)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:460)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)
... 25 more
Caused by: java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractPropertyMapping.getSuperCollection(AbstractPropertyMapping.java:285)
at org.hibernate.persister.entity.AbstractPropertyMapping.addPropertyPath(AbstractPropertyMapping.java:198)
at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:395)
at org.hibernate.persister.entity.AbstractEntityPersister.initOrdinaryPropertyPaths(AbstractEntityPersister.java:2300)
at org.hibernate.persister.entity.AbstractEntityPersister.initPropertyPaths(AbstractEntityPersister.java:2347)
at org.hibernate.persister.entity.AbstractEntityPersister.postConstruct(AbstractEntityPersister.java:3906)
at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>(JoinedSubclassEntityPersister.java:563)
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 org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
... 30 more
涉及的实体:
import javax.persistence.*;
@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idperson")
private Long idPerson;
@Column(name = "firstname")
private String firstName;
@Column(name = "lastname")
private String lastName;
@Column(name = "adress")
private String addresse;
@Column(name = "email")
private String email;
@Column(name = "mobile")
private String mobile;
@Column(name = "phone")
private String phone;
// Getters and setters
}
这是Person的子类:
import com.abdofx.demo.model.Person;
import com.abdofx.demo.model.tresorerie.Transaction;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "supplier")
@PrimaryKeyJoinColumn(name = "idperson")
public class Supplier extends Person {
@OneToMany(mappedBy = "supplier", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private Set<PurchasePiece> purchasePieces;
@OneToMany(mappedBy = "supplier", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private Set<Transaction> transactions;
}
这是子类客户端
import com.abdofx.demo.model.Person;
import com.abdofx.demo.model.tresorerie.Transaction;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "client")
@PrimaryKeyJoinColumn(name = "idperson")
public class Client extends Person {
@OneToMany(mappedBy = "client", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private Set<SellPiece> sellPieces;
@OneToMany(mappedBy = "sclient", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private Set<Transaction> transactions;
}
交易类:
import com.abdofx.demo.model.purchase.PurchasePiece;
import com.abdofx.demo.model.purchase.Supplier;
import com.abdofx.demo.model.sell.Client;
import com.abdofx.demo.model.sell.SellPiece;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "transaction")
public class Transaction {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idtransaction")
private Long idTransaction;
@Column(name = "datetransaction ")
private LocalDateTime dateTransaction;
@Column(name = "description")
private String description;
@Column(name = "value")
@Type(type = "org.hibernate.type.BigDecimalType")
private BigDecimal value;
@Enumerated(EnumType.STRING)
@Column(name = "direction")
private DirectionOfTransaction directionOfTransaction;
// Relationships
@ManyToOne()
@JoinColumn(name = "idpaymentmethod")
private paymentMethode paymentMethode;
@ManyToOne()
@JoinColumn(name = "idaccount")
private Account account;
@ManyToOne()
@JoinColumn(name = "sup_idperson")
private Supplier supplier;
@ManyToOne()
@JoinColumn(name = "idperson")
private Client sclient;
@ManyToOne()
@JoinColumn(name = "idtypetransaction")
private TypeOfTransaction typeOfTransaction;
@ManyToMany(mappedBy = "transactions", fetch = FetchType.EAGER)
Set<PurchasePiece> purchasePieces = new HashSet<>();
@ManyToMany(mappedBy = "selltransactions", fetch = FetchType.EAGER)
Set<SellPiece> sellPieces = new HashSet<>();
// Getter and setters
}
那么,造成这种问题的原因是什么?
答案 0 :(得分:2)
This is a bug with hibernate-core 5.2.14.Final。
紧急情况发布几周后,即对其进行了修复,5.2.15.Final。
您有两个解决方案。
如果您只需要替换有问题的hibernate-core,就可以替换为5.2.15.Final:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.15.Final</version>
</dependency>
否则,请使用较新版本的spring-jpa 2.0.6 or later。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>