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 collin
Messages: 7
Registered: February 2013
Junior Member
Hello,

I'm using EclipseLink 2.4.0 with MongoDB 2.2.
I've got a Entity which has a @OneToMany relation to itself :
@Entity
@NoSql(dataType="userGroup", dataFormat=DataFormatType.MAPPED)
public class Group extends JPAMongoBaseEntity {
	@Id
	@Field(name="_id")
	@GeneratedValue
	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)
	@Field(name="directSubGroups")
	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 Sutherland
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 Courtin
Messages: 4
Registered: April 2013
Junior Member
Hello,

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

import java.util.List;

@Entity
@NoSql(dataFormat=DataFormatType.MAPPED, dataType="personne")
public class Person {
	@Id
	@GeneratedValue
	@Field(name="_id")
	private String personId;

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

	@Embedded
	PersonalData personalData;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinColumn(name="ADDRESSES")
	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;

@Entity
@NoSql(dataFormat=DataFormatType.MAPPED, dataType="adresse")
public class Address {
	@Id
	@GeneratedValue
	@Field(name="_id")
	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.setPersonId("1");
	person.setPersonName("John Smith");
	person.setPersonalData(new PersonalData("john.smith@gmail.com",
			"www.johnsmith.com", "jsmith"));

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

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

	addresses.add(address1);
	addresses.add(address2);
	uow.registerNewObject(address1);
	uow.registerNewObject(address2);
    
	person.setAddresses(addresses);
 
    // register users in UnitOfWork
    uow.registerNewObject(person);
    // commit transaction
    uow.commit();
 
    // now try to read our users
    ReadAllQuery raq = new ReadAllQuery(Person.class);
    @SuppressWarnings("unchecked")
    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">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<class>info.noteo.Person</class>
		<class>info.noteo.Address</class>
		<properties>
			<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform" />
			<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec" />
			<property name="eclipselink.nosql.property.mongo.port" value="27017" />
			<property name="eclipselink.nosql.property.mongo.host" 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(EISOneToManyMapping.java:961)



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 Sutherland
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 Bhat
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.

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


Current Time: Mon Sep 22 06:28:54 GMT 2014

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

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