Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @manytomany and "Null primary key encountered in unit of work clone"
@manytomany and "Null primary key encountered in unit of work clone" [message #389785] Fri, 03 July 2009 12:24 Go to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
Dear all,

I have a problem in my project when I try to set a manytomany
relationship. I have two Entities: ObjectEntity and ExtConnectorEntity.
One ObjectEntity can be related to many ExtConnectorEntity and viceversa.
Unidirectional manytomany relationship is enough for me, because I am
interested to create, update and delete join basing on the ObjectEntity. I
mean...I want to add and delete record in the join table handling the
ObjectEntity entity. Anyway I tried also to set the bidirectional
relationship (in order to know if my problem solved, but I have yet the
same problem). I have a join table that is OBJECT_EXTCONNECTOR.

My code is:

@Entity
@Table(name="OBJECT")
public class ObjectEntity {
private int idObj; //the Object Id

//jpa relations
private List<ExtConnectorEntity> connectors;

public ObjectEntity() {
connectors = new ArrayList<ExtConnectorEntity>();
}

......

@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="OBJECT_EXTCONNECTOR",
joinColumns=
@JoinColumn(name="IDOBJ", referencedColumnName="IDOBJ"),
inverseJoinColumns=
@JoinColumn(name="IDCONN", referencedColumnName="IDCONN")
)
public List<ExtConnectorEntity> getConnectors() {
return connectors;
}

....other getter and setter

}


@Entity
@Table(name="EXTCONNECTOR")
public class ExtConnectorEntity {

...variables

//jpa relationship
private List<ObjectEntity> objects;


public ExtConnectorEntity() {
objects = new ArrayList<ObjectEntity>();
}

@ManyToMany(mappedBy="connectors",fetch=FetchType.LAZY)
public List<ObjectEntity> getObjects() {
return objects;
}

public void setObjects(List<ObjectEntity> objects) {
this.objects = objects;
}

...other getters and setters

}


I want to add and remove (update) entry in the JOIN table. I have DAO
classes I use to load, editù, delete etc..:

//load an object:
obj = objectDAO.findById(objid); //this load the object entity and
connectors list if any for this object id

//add or remove connectors from the object connector list. For example
connector =connectorDAO.findById(connID);
obj.getConnectors().add(connector); //add the connector to the object list

//finally I commit passing the object to a function that instantiate,
begin, //commit and finally close the entity manager.


Note that for every operation (findByID, commit etc..) in the DAO (the DAO
method) create and close an entity manager (so for update I have to merge
detache objects).

Running my code, when I try to create a new relation (adding a connecotr
to the object-connectors list) I have the folllowing error:
Null primary key encountered in unit of work clone
[ExtConnectorEntity@162b702]

Take in mind that I had also set the object relationship on both sides
(ObjectEntity and ExternalConnector], that is:

obj.getConnectors().add(connector);
connector.setObject(obj);

Anyone can help me, please?

Thanks in advance,
Enrico
Re: @manytomany and "Null primary key encountered in unit of work clone" [message #389786 is a reply to message #389785] Fri, 03 July 2009 15:26 Go to previous message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
Enrico ha scritto:
> Dear all,
>
> I have a problem in my project when I try to set a manytomany
> relationship. I have two Entities: ObjectEntity and ExtConnectorEntity.
> One ObjectEntity can be related to many ExtConnectorEntity and
> viceversa. Unidirectional manytomany relationship is enough for me,
> because I am interested to create, update and delete join basing on the
> ObjectEntity. I mean...I want to add and delete record in the join table
> handling the ObjectEntity entity. Anyway I tried also to set the
> bidirectional relationship (in order to know if my problem solved, but I
> have yet the same problem). I have a join table that is
> OBJECT_EXTCONNECTOR.
>
> My code is:
>
> @Entity
> @Table(name="OBJECT")
> public class ObjectEntity {
> private int idObj; //the Object Id
>
> //jpa relations
> private List<ExtConnectorEntity> connectors;
>
> public ObjectEntity() {
> connectors = new ArrayList<ExtConnectorEntity>();
> }
>
> ......
>
> @ManyToMany(fetch=FetchType.LAZY)
> @JoinTable(name="OBJECT_EXTCONNECTOR",
> joinColumns=
> @JoinColumn(name="IDOBJ", referencedColumnName="IDOBJ"),
> inverseJoinColumns=
> @JoinColumn(name="IDCONN", referencedColumnName="IDCONN")
> )
> public List<ExtConnectorEntity> getConnectors() {
> return connectors;
> }
> ....other getter and setter
>
> }
>
>
> @Entity
> @Table(name="EXTCONNECTOR")
> public class ExtConnectorEntity {
>
> ...variables
>
> //jpa relationship
> private List<ObjectEntity> objects;
>
>
> public ExtConnectorEntity() {
> objects = new ArrayList<ObjectEntity>();
> }
>
> @ManyToMany(mappedBy="connectors",fetch=FetchType.LAZY)
> public List<ObjectEntity> getObjects() {
> return objects;
> }
>
> public void setObjects(List<ObjectEntity> objects) {
> this.objects = objects;
> }
>
> ...other getters and setters
>
> }
>
>
> I want to add and remove (update) entry in the JOIN table. I have DAO
> classes I use to load, editù, delete etc..:
>
> //load an object:
> obj = objectDAO.findById(objid); //this load the object entity and
> connectors list if any for this object id
>
> //add or remove connectors from the object connector list. For example
> connector =connectorDAO.findById(connID);
> obj.getConnectors().add(connector); //add the connector to the object list
>
> //finally I commit passing the object to a function that instantiate,
> begin, //commit and finally close the entity manager.
>
>
> Note that for every operation (findByID, commit etc..) in the DAO (the
> DAO method) create and close an entity manager (so for update I have to
> merge detache objects).
>
> Running my code, when I try to create a new relation (adding a connecotr
> to the object-connectors list) I have the folllowing error:
> Null primary key encountered in unit of work clone
> [ExtConnectorEntity@162b702]
>
> Take in mind that I had also set the object relationship on both sides
> (ObjectEntity and ExternalConnector], that is:
>
> obj.getConnectors().add(connector);
> connector.setObject(obj);
>
> Anyone can help me, please?
>
> Thanks in advance,
> Enrico
>
>
>
>
>
>

Dear All,

I solved the problem. A field value in the ExtConntectorEntity was null
(not the primary key but another field). So, it was an error on the
table data and not on the java code.

Best regards,
Enrico
Previous Topic:[Teneo] Rollback with a EclipseLinkResourceImpl
Next Topic:Persistence context lost after rollback
Goto Forum:
  


Current Time: Sat Nov 29 09:03:48 GMT 2014

Powered by FUDForum. Page generated in 0.02060 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software