Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » ManyToMany mapping created when no mapping is specified
ManyToMany mapping created when no mapping is specified [message #1032651] Wed, 03 April 2013 09:00 Go to next message
Saravanan Subiramaniam is currently offline Saravanan Subiramaniam
Messages: 10
Registered: April 2013
Junior Member
Hi,
In my Entity, when I don't specify the mapping for a collection attribute, by default ManyToMapping is created. Won't an exception be thrown in this case that the mapping is missing? Or is the default behavior to create ManyToMany mapping for collection attribute?

Eg:

public class Parent{
private List<Child> childList;

public List<Child> getChildList(){
return childList;
}

public void setChildList(List<Child> childList){
this.childList = childList;
}
}

The rational for not specifying the mapping is that, I would like to create OneToManyMapping programmatically in the session customizer pointing mappedBy to an entity whose reverse mapping is not known at static weaving time.
Re: ManyToMany mapping created when no mapping is specified [message #1034430 is a reply to message #1032651] Fri, 05 April 2013 12:22 Go to previous messageGo to next message
Saravanan Subiramaniam is currently offline Saravanan Subiramaniam
Messages: 10
Registered: April 2013
Junior Member
One more thing: Parent is a MappedSuperclass.
Re: ManyToMany mapping created when no mapping is specified [message #1034693 is a reply to message #1034430] Fri, 05 April 2013 19:35 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1023
Registered: July 2009
Senior Member
The JPA specification requires field/properties to use default mappings when one isn't specified. You can either mark the field/property with @Transient, or just replace the default mapping with the OneToManyMapping in the customizer.
Re: ManyToMany mapping created when no mapping is specified [message #1037892 is a reply to message #1034693] Wed, 10 April 2013 06:13 Go to previous messageGo to next message
Saravanan Subiramaniam is currently offline Saravanan Subiramaniam
Messages: 10
Registered: April 2013
Junior Member
Hi Chris,
What do you mean by replace the default mapping with OneToManyMapping in the session customizer, remove the mapping and add?

classDescriptor.getMappings().remove(classDescriptor.getMappingForAttributeName("childList"));
classDescriptor.addMapping(newOneToManyMapping);


Also, I tried marking the attribute with @Transient and creating OneToManyMapping (as given below).

@Transient
private List<Child> childList;


And in the customizer,
OneToManyMapping oneToManyMapping = new OneToManyMapping();
oneToManyMapping.setAttributeName("childList");
oneToManyMapping.useTransparentList();
oneToManyMapping.privateOwnedRelationship();
oneToManyMapping.setCascadeAll(TRUE);
oneToManyMapping.setReferenceClass(Parent.class);
oneToManyMapping.addTargetForeignKeyFieldName("PARENT_ID", "ID");
classDescriptor.addMapping(oneToManyMapping);

When I persist the parent along with childList, the childList is not persisted. Also when I query the parent and then invoke getChildList(), no sql stmt is fired. Looks like the mapping is incorrect. Could you please explain what is missing here? (Note that it works when I don't mark the attribute @Transient and replace the ManyToManyMapping with OneToManyMapping).

[Updated on: Wed, 10 April 2013 18:01]

Report message to a moderator

Re: ManyToMany mapping created when no mapping is specified [message #1042450 is a reply to message #1037892] Tue, 16 April 2013 13:04 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I would recommend using the add/remove, did you try this?

Making it @Transient can affect weaving, as the object is weaved before customizers are run, so the attribute will not get weaved. You could disable "eclipselink.weaving.internal", or disable weaving entirely. Not marking it @Transient, but adding/removing in the customizer is the better solution.


James : Wiki : Book : Blog : Twitter
Re: ManyToMany mapping created when no mapping is specified [message #1042459 is a reply to message #1042450] Tue, 16 April 2013 13:14 Go to previous message
Saravanan Subiramaniam is currently offline Saravanan Subiramaniam
Messages: 10
Registered: April 2013
Junior Member
Hi James,
Removing @Transient and remove existing mapping and add new mapping in the customizer works. Just wondering why it didn't work when the attribute is marked with @Transient (also tried by disabling eclipselink.weaving.internal). Just before the parent entity is persisted, if I get the server session and see the mapping for the child attribute, it is present (and with valid reference class). Is there a way to debug what/where the problem is?

Thanks,
Saravanan.

[Updated on: Tue, 16 April 2013 13:45]

Report message to a moderator

Previous Topic:[oracle] Using procedures to get the cursor not working
Next Topic:org.eclipse.persistence.eis.adapters package Missing in 2.4.0
Goto Forum:
  


Current Time: Tue Sep 23 16:27:01 GMT 2014

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

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