Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @OneToMany relations with @NOSQL don't works(Using @OneToMany relation with a @nosql database leads to errors)
@OneToMany relations with @NOSQL don't works [message #1014992] Wed, 27 February 2013 07:37 Go to next message
jm collin is currently offline jm collinFriend
Messages: 7
Registered: February 2013
Junior Member

I'm using EclipseLink 2.4.0 with MongoDB 2.2.
I've got a Entity which has a @OneToMany relation to itself :
@NoSql(dataType="userGroup", dataFormat=DataFormatType.MAPPED)
public class Group extends JPAMongoBaseEntity {
	private String id;
	// Only the direct subGroup are set (not the subGroup of the subGroup)
	@OneToMany(targetEntity=Group.class, fetch=FetchType.LAZY, cascade=CascadeType.REMOVE)
	private List<Group> directSubGroups = new ArrayList<>();

When I use this relation, items added to the relation - entity.getDirectSubGroups().add(...) are correctly send to database.
but when EclipseLink retrieve the entity from database, the relation is always empty.
So the next add will override the previous and only the last .add() will persist.

Any help will be greatly appreciated.

Re: @OneToMany relations with @NOSQL don't works [message #1016226 is a reply to message #1014992] Tue, 05 March 2013 14:47 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

If the relationship is correctly written, it is odd that it is empty on read. It could be a caching issue, trying refreshing.
Ensure that you are correctly maintain your objects when persisting, if a bi-directional relationship, ensure you set both sides.

James : Wiki : Book : Blog : Twitter
Re: @OneToMany relations with @NOSQL don't works [message #1053158 is a reply to message #1016226] Thu, 02 May 2013 18:26 Go to previous messageGo to next message
David Courtin is currently offline David CourtinFriend
Messages: 4
Registered: April 2013
Junior Member

I've got the same problem as jm. Here are my classes :
package info.noteo;

import java.util.List;

@NoSql(dataFormat=DataFormatType.MAPPED, dataType="personne")
public class Person {
	private String personId;

	@Column(name = "PERSON_NAME")
	private String personName;

	PersonalData personalData;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private List<Address> addresses;

	public String getPersonId() {
		return personId;

	public void setPersonId(String personId) {
		this.personId = personId;

	public String getPersonName() {
		return personName;

	public void setPersonName(String personName) {
		this.personName = personName;

	public PersonalData getPersonalData() {
		return personalData;

	public void setPersonalData(PersonalData personalData) {
		this.personalData = personalData;

	public List<Address> getAddresses() {
		return addresses;

	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;


package info.noteo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.Converter;
import org.eclipse.persistence.nosql.annotations.DataFormatType;
import org.eclipse.persistence.nosql.annotations.Field;
import org.eclipse.persistence.nosql.annotations.NoSql;

@NoSql(dataFormat=DataFormatType.MAPPED, dataType="adresse")
public class Address {
	private String addressId;
	private String street;

	public String getAddressId() {
		return addressId;

	public void setAddressId(String addressId) {
		this.addressId = addressId;

	public String getStreet() {
		return street;

	public void setStreet(String street) {
		this.street = street;

package info.noteo;

import java.util.ArrayList;
public class EclipseLinkTest {
  private static final String PERSISTENCE_UNIT_NAME = "nosql-pu";
  private final EntityManagerFactory entityManagerFactory;
  private final EntityManager entityManager;
  private final JpaEntityManager jpa;
  private final ServerSession serverSession;
  public EclipseLinkTest() {
    // initializations
    entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    entityManager = entityManagerFactory.createEntityManager();
    jpa = (JpaEntityManager) entityManager.getDelegate();
    serverSession = jpa.getServerSession();
  public UnitOfWork acquireUnitOfWork() {
    return serverSession.acquireClientSession().acquireUnitOfWork();
  public static void main(String[] args) {
	  EclipseLinkTest main = new EclipseLinkTest();
    UnitOfWork uow = main.acquireUnitOfWork();
    // create 1 person and 2 address
    Person person = new Person();
	person.setPersonName("John Smith");
	person.setPersonalData(new PersonalData("",
			"", "jsmith"));

	List<Address> addresses = new ArrayList<Address>();
	Address address1 = new Address();
	address1.setStreet("123, Old street");

	Address address2 = new Address();
	address2.setStreet("456, New street");

    // register users in UnitOfWork
    // commit transaction
    // now try to read our users
    ReadAllQuery raq = new ReadAllQuery(Person.class);
    String id = "";
    List<Person> users = (List<Person>) uow.executeQuery(raq);
    for (Person u : users) {
      System.out.println("Person: " + u.getPersonName());
      System.out.println("Identifiant Person :"+u.getPersonId());
      id = u.getPersonId();
    // recherche une personne avec son ID
    Person p1= main.entityManager.find(Person.class, id);
    p1.getAddresses().get(0); // this throw and Exception !

<persistence-unit name="nosql-pu" transaction-type="RESOURCE_LOCAL">
			<property name="" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform" />
			<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec" />
			<property name="" value="27017" />
			<property name="" value="localhost" />

If fetch=FetchType.LAZY defined in @OneToMany annotation, collection is not initialized.

If fetch=FetchType.EAGER defined in @OneToMany annotation, an Exception is thrown :Exception in thread "main" java.lang.ClassCastException: org.eclipse.persistence.sessions.DatabaseRecord cannot be cast to org.eclipse.persistence.oxm.record.XMLRecord
at org.eclipse.persistence.eis.mappings.EISOneToManyMapping.valueFromRow(

Re: @OneToMany relations with @NOSQL don't works [message #1053778 is a reply to message #1053158] Tue, 07 May 2013 14:44 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Please log a bug for these issues. Include the full exception stack trace and your model code.

Normally in Mongo the address objects would be embedded in the person structure. To do this an ElementCollection mapping would be used, and the Address would be mapped as Embeddable.

James : Wiki : Book : Blog : Twitter
Re: @OneToMany relations with @NOSQL don't works [message #1384806 is a reply to message #1053778] Mon, 02 June 2014 08:34 Go to previous message
Murthy Bhat is currently offline Murthy BhatFriend
Messages: 159
Registered: July 2009
Senior Member
Hello Mr. David Courtin,

Were you able to resolve the @OneToMany relation related Exception ? If so could you share some details how you fixed it ?

I am facing a similar issue with @OneToMany relation. Though I got it working with FetchType.LAZY and then manually trying to load eagerly(list.size()), I want to understand if is this a bug or am I doing something wrong. Could someone please help me understand.

Previous Topic:positional parameter in query
Next Topic:EL issues SELECT 1 after every exception [SYBASE]
Goto Forum:

Current Time: Fri Jun 22 00:11:05 GMT 2018

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

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

Back to the top