JPA多继承无法创建架构

时间:2018-06-18 08:30:29

标签: java jpa eclipselink

我有一个包含多层继承的数据模型。只要每个层只包含一个类,我就可以生成数据库模式。当我向最后一层添加另一个类时,我在尝试生成模式时遇到异常:

Exception Description: Predeployment of 
  PersistenceUnit [JPA] failed.
Internal Exception: java.lang.NullPointerException

我的数据模型实现如下:

PersistentEntiy.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter(value = AccessLevel.PROTECTED)
@Setter(value = AccessLevel.NONE)
@MappedSuperclass
@IdClass(DatabaseId.class)
public abstract class PersistentEntity {

  @Id
  @Column(name = "ID", nullable = false)
  @NonNull
  @Setter(value = AccessLevel.NONE)
  private UUID id;

  @Id
  @Column(name = "LAST_MODIFIED", nullable = false)
  @NonNull
  @Setter(value = AccessLevel.NONE)
  private LocalDateTime lastModified;

}

Booking.java

@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@Setter(AccessLevel.NONE)
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "BOOKINGS", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"TARGETCASHREGISTER", "BOOKINGNUMBER"})
})
public class Booking extends PersistentEntity {

  @Column(nullable = false, precision = 36, scale = 2)
  @DecimalMin("0.01")
  @NonNull
  private BigDecimal amount;
  @Column(nullable = false)
  @NonNull
  private String text;
  @Column(nullable = false)
  private boolean isInPayment;
  @Column(nullable = false)
  @NonNull
  private UUID targetCashRegister;
  @Column(nullable = false)
  private long bookingNumber;
  @Column(nullable = false)
  @NonNull
  private String affiliate;
  @Transient
  private boolean isAnnulated = false;

  ...
}

CashContribution.java

@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Entity
@Table(name = "CASHCONTRIBUTIONS")
public class CashContribution extends Booking {
  ...
}

在此之前一切正常。但是在添加以下类时,模式生成失败。

CashRetrieval.java

@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Entity
@Table(name = "CASHRETRIEVALS")
public class CashRetrieval extends Booking {
  ...
}

注意:此项目大量使用lombok注释在编译时生成代码。

任何人都可以帮我解决这个问题吗?

编辑1:完整堆栈跟踪

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with 
ClassLoader: sun.misc.Launcher$AppClassLoader@18b4aac2
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [JPA] failed.
Internal Exception: java.lang.NullPointerException
  at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
  at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:115)
  at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:188)
  at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
  at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
  at com.gipa.kassenbuch.Main.<init>(Main.java:16)
  at com.gipa.kassenbuch.Main.main(Main.java:26)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [JPA] failed.
Internal Exception: java.lang.NullPointerException
  at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:2080)
  at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:2071)
  at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:101)
  at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:104)
  ... 5 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [JPA] failed.
Internal Exception: java.lang.NullPointerException
  at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:231)
  ... 9 more
Caused by: java.lang.NullPointerException
  at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.setPKClass(MetadataDescriptor.java:1846)
  at org.eclipse.persistence.internal.jpa.metadata.inheritance.InheritanceMetadata.process(InheritanceMetadata.java:244)
  at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processTableAndInheritance(EntityAccessor.java:1390)
  at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(EntityAccessor.java:683)
  at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1876)
  at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:575)
  at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:610)
  at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1992)
  ... 7 more

Main.java

public class Main {

  private final EntityManager manager;

  public Main () {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA");
    manager = factory.createEntityManager();
  }

  public void run() {
    final DatabaseProvider provider = DatabaseProvider.getInstance();
  }

  public static void main(String[] args) {
    new Main().run();
  }

}

1 个答案:

答案 0 :(得分:1)

堆栈跟踪显示它无法在继承根实体中找到IdClass。这似乎是mappingsupperclass解析中的错误,可以通过将@IdClass(DatabaseId.class)批注放在Booking类上来解决。