创建表格表单JPA实体失败

时间:2014-05-14 09:16:37

标签: jpa eclipselink

我在Eclipse(Kepler)中设置了一个全新的JPA项目......在我遇到现有项目中的问题之后。

我打算编写一个JpaEntity,让Eclipse(或JPA分别)在数据库中创建相应的表,但这失败了,我无法弄清楚原因。 Eclipse告诉我表" myentrances"无法解决。当然......它现在还不存在。 ......它应该被创造出来。

这是我的JpaEntity:

@Entity
@Table(name="myentrances")
@NamedQuery(name="MyEntrance.findAll", query="SELECT e FROM MyEntrance e")
@TableGenerator(name = "myentrances")
@Access(PROPERTY)
public class MyEntrance implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int entranceId;

    @OneToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER, orphanRemoval = true)
    @PrimaryKeyJoinColumn
    private Address address;

    @Column(nullable=false)
    private double maxHeight;

    @Column(nullable=false)
    private String maxHeightUom;

    private static final long serialVersionUID = 1L;

    public MyEntrance() {
        super();
    } 

    public int getEntranceId() {
        return this.entranceId;
    }

    public void setEntranceId(int entranceId) {
        this.entranceId = entranceId;
    }

    public Address getAddress() {
        return this.address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public double getMaxHeight() {
        return this.maxHeight;
    }

    public void setMaxHeight(double maxHeight) {
        this.maxHeight = maxHeight;
    }

    public String getMaxHeightUom() {
        return this.maxHeightUom;
    }

    public void setMaxHeightUom(String maxHeightUom) {
        this.maxHeightUom = maxHeightUom;
    }
}

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JpaTest" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.tsystems.ikt.model.Address</class>
    <class>com.tsystems.ikt.model.MyEntrance</class>
    <properties>
        <property name="eclipselink.ddl-generation" value="create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="database"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myDb"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
    </properties>
</persistence-unit>

这是我得到的例外:

[EL Config]: metadata: The element [method getAddress] is being defaulted to a one to one mapping.
[EL Config]: metadata: The target entity (reference) class for the one to one mapping element [method getAddress] is being defaulted to: class com.tsystems.ikt.model.Address.
[EL Config]: metadata: The access type for the persistent class [class com.tsystems.ikt.model.Address] is set to [FIELD].
[EL Config]: metadata: The access type for the persistent class [class com.tsystems.ikt.model.MaiEntrance] is set to [FIELD].

[EL Config]: metadata: The alias name for the entity class [class com.tsystems.ikt.model.MyEntrance] is being defaulted to: MyEntrance.
[EL Config]: metadata: The column name for element [getEntranceId] is being defaulted to: ENTRANCEID.
[EL Config]: metadata: The column name for element [getMaxHeightUom] is being defaulted to: MAXHEIGHTUOM.
[EL Config]: metadata: The column name for element [getMaxHeight] is being defaulted to: MAXHEIGHT.
Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: org.eclipse.persistence.dynamic.DynamicClassLoader@580760d7
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [JpaTest] failed.
Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class com.tsystems.ikt.model.MyEntrance] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:107)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.buildEntityManagerFactory(Main.java:94)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:80)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:68)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [JpaTest] failed.
Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class com.tsystems.ikt.model.MyEntrance] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1954)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1945)
at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:98)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:96)
... 5 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [JpaTest] failed.
Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class com.tsystems.ikt.model.MyEntrance] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.
at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:230)
... 9 more
Caused by: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class com.tsystems.ikt.model.MyEntrance] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.
at org.eclipse.persistence.exceptions.ValidationException.noPrimaryKeyAnnotationsFound(ValidationException.java:1422)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.validatePrimaryKey(EntityAccessor.java:1536)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processMappingAccessors(EntityAccessor.java:1243)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(EntityAccessor.java:697)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1793)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:576)
at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:585)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1869)
... 7 more   

我只使用字段访问(也在地址中)。

我正在使用Eclipselink 2.5.1。

顺便说一下: 是不是可以为一个新创建的JpaEntity创建一个表?我是否必须选择MyProject - &gt; JPA工具 - &gt;从EntitieS生成TableS?这似乎也创建了所有现有的表,是吗?如果我已经创建了一堆表,现在只想基于新创建的JpaEntity添加另一个表,该怎么办?

1 个答案:

答案 0 :(得分:0)

JPA中实体的默认访问模式AccessType.FIELD

您已通过为实体声明@Access(PROPERTY)来覆盖它,因此在处理元数据时,持久性提供程序不会注意到@Id注释。尝试删除@Access(PROPERTY)注释以解决问题。

  

如果我已经创建了一堆表,现在只是想要怎样   根据新创建的JpaEntity添加另一个表?

这取决于一个场景:

  • 如果出于任何原因(即遗留数据库的情况下)禁止删除现有表:在create-tables中保留persistence.xml,以便提供者基于新实体(即MyEnterance)创建表;对于现有的表,您可以稍后使用DROP TABLE myentrances删除相应的表并在下次启动时自动重新创建 - 提供程序将负责从头开始重新创建实体
  • 如果允许删除现有表:在persistence.xml中将create-tables替换为drop-and-create-tables - 提供程序将负责从头开始重新创建整个实体模型