@OneToMany relations with @NOSQL don't works [message #1014992] |
Wed, 27 February 2013 02:37  |
Eclipse User |
|
|
|
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 #1053158 is a reply to message #1016226] |
Thu, 02 May 2013 14:26   |
Eclipse User |
|
|
|
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 #1384806 is a reply to message #1053778] |
Mon, 02 June 2014 04:34  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.03953 seconds