2

I have a strange problem with eclipseLink and an object which I want to persist. I have one Object (KeypointListImpl) that stores another object KeypointImpl in a List. Persisting a keypointImpl objects works great but if I try to persist a keypointListImpl object I get an java.lang.IllegalArgumentException that says the object keypointImpl isn't a known entity type.

Here is the KeypointImpl Code:

@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;
    ...
}

Here is the KeypointListImpl Code:

@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;
    ...
}

Here is the mains content:

    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);

The tables that are constructed look fine. I get a KeypointsLists(ID, KEYPOINTS_ID) and a Keypoints(..., ...) table.

Can anyone point me to my error please?! :-)

As requested the persistence.xml as well

<?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>
Hans Sperker
  • 1,347
  • 2
  • 15
  • 37
  • @OneToOne on `List` looks wrong. Use @OneToMany or smth. After fix post your results. – popfalushi Jun 09 '12 at 12:17
  • Can you post your persistence.xml? – siebz0r Jun 09 '12 at 12:19
  • OneToOne at this point just means that one keypoint is only associated to one Keypointlist entry and vice versa – Hans Sperker Jun 09 '12 at 12:58
  • @popfalushi - the @OneTo@Many results i a KeypointsList table that only has an id field, a KeypointList_Keypoints table that joins the ids and a Keypoint table as well as a javax.persistence.RollbackException -> Exception Description: The list of fields to insert into the table [DatabaseTable(KeypointLists)] is empty. You must define at least one mapping for this table – Hans Sperker Jun 09 '12 at 13:05
  • 1
    `@OneToOne` can't be `Collection` (quite obviously). Either it is `@OneToMany` or it is not a `Collection`. I'm not sure that that is the whole problem though. I've never seen Entities hold relationships to interfaces (of another entity) - but it might just work. – esej Jun 09 '12 at 13:18
  • Ok seems like I have done a mistake. The association KeypointList -> Keypoint should be that the KeypointList table just has an id and that is a foreign key in the keypoint table! hmmm – Hans Sperker Jun 09 '12 at 13:19
  • ok it works I have made a mistake. The solution is a @OneToMany annotation (thanks @popfalushi) with a mappedBy= and a reference from keypoint to keypointlist ;-) – Hans Sperker Jun 09 '12 at 13:48
  • @jstr well then don't forget to accept the answer. – zellus Jun 09 '12 at 19:53
  • include the exception and stack trace – James Jun 11 '12 at 13:53

1 Answers1

4

Than I'll write my comment as an answer: @OneToOne on List looks wrong. Use @OneToMany.

popfalushi
  • 1,332
  • 9
  • 15