2

我对 eclipseLink 和一个我想坚持的对象有一个奇怪的问题。我有一个对象 (KeypointListImpl) 将另一个对象 KeypointImpl 存储在列表中。持久化一个 keypointImpl 对象效果很好,但是如果我尝试持久化一个 keypointListImpl 对象,我会得到一个 java.lang.IllegalArgumentException,它说对象 keypointImpl 不是已知的实体类型。

这是 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>
4

1 回答 1

4

比我写我的评论作为答案:@OneToOne on List 看起来不对。使用@OneToMany。

于 2012-06-09T14:06:53.860 回答