java - EclipseLink JPA 对象不是已知的实体类型

标签 java mysql jpa eclipselink

我对 eclipseLink 和一个我想保留的对象有一个奇怪的问题。我有一个对象 (KeypointListImpl),它将另一个对象 KeypointImpl 存储在列表中。保留 keypointImpl 对象效果很好,但如果我尝试保留 keypointListImpl 对象,我会收到 java.lang.IllegalArgumentException ,表明该对象 k​​eypointImpl 不是已知的实体类型。

这是 KeypointImpl 代码:

@Entity
@Table(name="Keypoints")
public class KeypointImpl implements Keypoint {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Enumerated(EnumType.STRING)
    private DetectorType keypointType;
    private float x;
    private float y;
    private float size;
    private float angle;
    private float response;
    private int octave;
    private int classId;
    ...
}

这是 KeypointListImpl 代码:

@Entity
@Table(name="KeypointLists")
public class KeypointListImpl implements KeypointList {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @OneToOne(cascade={CascadeType.ALL}, targetEntity=KeypointImpl.class)
    private List<Keypoint> keypoints;
    ...
}

主要内容如下:

    Keypoint kp1 = new KeypointImpl(DetectorType.FAST, 5, 5, 10, 90, 2, 3, 0);
    Keypoint kp2 = new KeypointImpl(DetectorType.FAST, 6, 6, 3, 45, 1, 2, 1);

    em.persist(kp1);
    em.persist(kp2);

    List<Keypoint> keypoints = new ArrayList<Keypoint>();
    keypoints.add(kp1);
    keypoints.add(kp2);

    KeypointList keypointlist = new KeypointListImpl();
    keypointlist.setKeypointList(keypoints);

    em.persist(keypointlist);

构建的表看起来不错。我得到一个 KeypointsLists(ID, KEYPOINTS_ID) 和一个 Keypoints(..., ...) 表。

任何人都可以指出我的错误吗?! :-)

根据要求还有 persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="AudiModelRecognition" transaction-type="RESOURCE_LOCAL">

        <class>amr.model.KeypointImpl</class>
        <class>amr.model.KeypointListImpl</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/amr" />
            <property name="javax.persistence.jdbc.user" value="arm" />
            <property name="javax.persistence.jdbc.password" value="..." /> 

            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
        </properties>

    </persistence-unit>
</persistence>

最佳答案

然后我会写下我的评论作为答案:列表上的@OneToOne 看起来是错误的。使用@OneToMany。

关于java - EclipseLink JPA 对象不是已知的实体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10960882/

相关文章:

MySQL - 查询帮助选择具有按日期分组的最新时间戳的结果

mysql - SQL 查询没有返回任何值

mysql - 转换 Azure Web App MySql 实现

java - eclipselink (JPA) 异常 : composite primary key @JoinColumn

java - 不可能在 ArrayList.get() 上发生 IndexOutOfBound。有什么线索吗?

c# - 两次调用同一个 C# 属性是不是不好的编程习惯?

java - @SequenceGenerator 的 initValue 和 AllocationSize 被忽略,生成器未分配给 @Id 字段(H2、HIbernate、Spring)

java - 使用 JPA 将 LocalDate 存入数据库

java - org.hibernate.TypeMismatchException : Provided id of the wrong type while fetching data

java - 从字符串中删除注释