2014-05-14 97 views
0

我在Eclipse(Kepler)中創建了一個全新的JPA項目......在遇到現有項目中的問題後。我打算寫一個JpaEntity,並讓Eclipse(或JPA分別)在數據庫中創建相應的表,但是這失敗了,我找不出原因。 Eclipse告訴我表「myentrances」無法解析。當然......現在不存在。它將被創建。創建表格JPA實體失敗

這裏是我的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: [email protected] 
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 

訪問(在Addre ss)。

我正在使用Eclipselink 2.5.1。

順便說一句: 是不是可以爲一個新創建的JpaEntity創建一個表?我是否必須選擇MyProject - > JPA工具 - >從EntitieS生成表格?這似乎也創建了所有現有的表,是嗎?如果我已經創建了一堆表,現在只想添加一個基於新創建的JpaEntity的表?

回答

0

對於JPA中的實體,默認訪問模式AccessType.FIELD

您已通過爲實體聲明@Access(PROPERTY)來覆蓋它,因此@Id註解在處理元數據時未被持久性提供程序注意到。嘗試刪除@Access(PROPERTY)註釋以解決問題。

如果我已經創建了一堆表,現在只想 添加一個基於新創建的JpaEntity的表?

這取決於從一個場景:

  • 如果下探現有表禁止以任何理由(即舊的數據庫的情況下):離開create-tables在persistence.xml中,這樣的供應商創建一個基於表在一個新的實體(即MyEnterance)上;對於已經存在的表,你可以使用DROP TABLE myentrances後來就刪除相應的表,並自動重新創建它在未來啓動 - 如果丟棄現有的表被允許供應商將採取從頭
  • 重新創建實體的護理:用drop-and-create-tables取代create-tables在persistence.xml中 - 提供程序將負責從零開始重新創建整個實體模型
+0

謝謝,wypieprz。我現在能夠創建這個新表'myentrance'。關於爲新的JpaEntities創建表...是否會在persistence.xml中調整管理實體部分(也許只有一個新的JpaEntity條目)? –

+0

不客氣。我稍微更新了我的答案 - 參見上文......關於這個問題 - 託管實體的定義可以在[映射文件]中完全配置(http://help.eclipse.org/kepler/index.jsp?topic= /org.eclipse.jpt.doc.user/task_manage_orm.htm)(即orm.xml),另一方面persistence.xml可用於指定給定的持久性提供程序如何生成數據庫模式。 – wypieprz

+0

但這不是使用註釋的首選方式嗎? 但是,記住create-table只創建新表但不丟棄其他表是很好的提示。因此,項目 - > JPA工具 - >從實體創建表...確實只考慮我的新JpaEntities。 –