我在通过ANT构建脚本为我的模型生成泛型时遇到了这个错误:
Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class com.ckd.model.BookModel] 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.
这是我的实体类:
@Entity
@Table(name = "BOOK")
public class BookModel implements Serializable
{
private static final long serialVersionUID = 1L;
// @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private Long id;
@Id
@NotNull
@Size(min = 32, max = 32)
@Column(name = "HASHID", unique = true, nullable = false)
private String hashid;
@NotNull
@Size(min = 1, max = 255)
@Column(name = "TITLE")
public String title;
@NotNull
@Size(min = 1, max = 255)
@Column(name = "AUTHOR")
private String author;
@Lob
@NotNull
@Column(name = "CONTENT")
private String content;
@Size(min = 2, max = 3)
@Column(name = "LANG")
public String lang;
// getters and setters here
}
我也将此添加到我的实体中,但没有区别:
@Entity
@Access(AccessType.FIELD)
针对Glassfish 3.1附带的捆绑EclipseLink库或使用http://www.eclipse.org/eclipselink/downloads/中的EclipseLink v2.x.x独立库构建我的项目也没有任何区别 - 仍然遇到上述错误。
根据Java EE项目要求,我的persistence.xml
位于WebContent\META-INf
目录下。它的内容是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="persistentUnit" transaction-type="JTA">
<description>Persistent Unit for Entity Classes</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/prod1</jta-data-source>
<class>com.ckd.model.BookModel</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.jdbc.DBDictionary" value="mysql(DriverVendor=mysql)" />
<property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform" />
<property name="eclipselink.jdbc.DBDictionary" value="searchStringEscape=\\" />
<property name="eclipselink.jdbc.QuerySQLCache" value="false" />
<property name="eclipselink.weaving" value="false" />
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog" />
<property name="eclipselink.logging.level" value="FINEST" />
</properties>
</persistence-unit>
</persistence>
如何解决此错误?这是EclipseLink 2.x.x的当前实现的错误吗?
答案 0 :(得分:0)
orm.xml
中的一个未使用的实体条目导致了上述错误。注释掉未使用的实体条目,修复了上述错误。
答案 1 :(得分:0)
您必须取消注释班级中的@Id注释
private static final long serialVersionUID = 1L;
// @Id <------ HERE
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private Long id;
......