Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » EclipseLink 2.0 ManyToOne not working correctly
EclipseLink 2.0 ManyToOne not working correctly [message #636193] Fri, 29 October 2010 15:21 Go to next message
Edmondo is currently offline EdmondoFriend
Messages: 11
Registered: October 2010
Junior Member
Dear all,
I am meeting a problem with EclipseLink 2.0 .

I create some entities and I persist them and they are persisted correctly. When I load them , however, a field is null. What do you suggest?

@Entity
public class Curve implements Serializable {

@Id
private String curveName;
private String description;
private static final long serialVersionUID = 1L;
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name = "CURRENCY_CURRENCYNAME")

private Currency currency;
@ManyToMany(mappedBy = "associatedCurves",fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Security> securityList = new ArrayList<Security>();
@OneToMany(fetch=FetchType.EAGER)
private List<CurvePoint> curvePoints;
@Transient
private GotwareDataObject gotwareDataObject;
// Getters and setters obmitted
}

public class Currency implements Serializable {
@OneToMany(mappedBy="currency",fetch=FetchType.EAGER,cascade=CascadeType.ALL)
List<Curve> curves;
private static final long serialVersionUID = 1L;
@Id
String currencyName;
}

My problem is the following:
I create a currency Euro and a curve A.
I added a curve A to the currency Euro and I set Euro as the currency of A. This is correctly mapped in the database when I persist.

When I load with a em.find a certain currency, the List of Curve it's correctly populated. However, inside each curve, the currency is null.

What can I do?

Thank you very much
Edmondo
Re: EclipseLink 2.0 ManyToOne not working correctly [message #636476 is a reply to message #636193] Mon, 01 November 2010 14:12 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Odd, I can't see why this would occur. Please include the code you use to persist, also include your get/set/add methods and ensure that they are working correctly. Ensure you are setting both sides of the relationship correctly.

If you refresh the Curve, does it get its currency?


James : Wiki : Book : Blog : Twitter
Re: EclipseLink 2.0 ManyToOne not working correctly [message #636480 is a reply to message #636476] Mon, 01 November 2010 14:33 Go to previous messageGo to next message
Edmondo is currently offline EdmondoFriend
Messages: 11
Registered: October 2010
Junior Member
public void test(){
List<Security> securityList = new ArrayList<Security>();

Currency currency = new Currency(GotwareDatabaseConstants.CURRENCY_AREA_EURO);

EoniaSwap eonia1 = new EoniaSwap();
eonia1.setName("Eonia 1 week swap");
eonia1.setDescription("Daily fixing compounded over period stipulated");
eonia1.setTicker("EUSWE1Z Curncy");
BigDecimal bigDecimal1= new BigDecimal("0.25");
eonia1.getMarketData().setLastPrice(bigDecimal1);
securityList.add(eonia1);
em.persist(eonia1);

EoniaSwap eonia2 = new EoniaSwap();
eonia2.setName("Eonia 2 week swap");
eonia2.setDescription("Daily fixing compounded over period stipulated");
eonia2.setTicker("EUSWE2Z Curncy");
BigDecimal bigDecimal2= new BigDecimal("0.30");
eonia1.getMarketData().setLastPrice(bigDecimal2);
securityList.add(eonia2);
em.persist(eonia2);

EoniaSwap eonia3 = new EoniaSwap();
eonia3.setName("Eonia 3 week swap");
eonia3.setDescription("Daily fixing compounded over period stipulated");
eonia3.setTicker("EUSWE3Z Curncy");
BigDecimal bigDecimal3= new BigDecimal("0.35");
eonia1.getMarketData().setLastPrice(bigDecimal3);
securityList.add(eonia3);
em.persist(eonia3);
em.flush();


Curve eoniaCurve = new Curve(securityList);
eoniaCurve.setCurveName("Pippo pluto");
eoniaCurve.setCurrency(currency);
CurvePoint curvePoint1 = new CurvePoint("GOTX1234",new BigDecimal(0.73));
CurvePoint curvePoint2 = new CurvePoint("GOTX1235",new BigDecimal(0.75));
CurvePoint curvePoint3 = new CurvePoint("GOTX1236",new BigDecimal(0.77));
List<CurvePoint> curvePoints = new ArrayList<CurvePoint>();
curvePoints.add(curvePoint1);
curvePoints.add(curvePoint2);
curvePoints.add(curvePoint3);
curvePoint1.setCurve(eoniaCurve);
curvePoint2.setCurve(eoniaCurve);
curvePoint3.setCurve(eoniaCurve);

List<Curve> curves = new ArrayList<Curve>();
curves.add(eoniaCurve);
eonia1.setAssociatedCurves(curves);
eonia2.setAssociatedCurves(curves);
eonia3.setAssociatedCurves(curves);

em.persist(eoniaCurve);


currency.setCurves(curves);
em.persist(currency);
em.flush();

}


This is my persisting code...do you need the getter and the setter of the classes as well?
Re: EclipseLink 2.0 ManyToOne not working correctly [message #636645 is a reply to message #636480] Tue, 02 November 2010 10:29 Go to previous messageGo to next message
Edmondo is currently offline EdmondoFriend
Messages: 11
Registered: October 2010
Junior Member
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="Gotware-jpa">
<jta-data-source>jdbc/gottexDataSource</jta-data-source>
<class>gotware.finance.ois.Ois</class>
<class>gotware.finance.ois.EoniaSwap</class>
<class>gotware.finance.fra.ForwardRateAgreement</class>
<class>gotware.finance.euribors.Euribor</class>
<class>gotware.finance.curves.Curve</class>
<class>gotware.finance.currency.Currency</class>
<class>gotware.finance.base.Security</class>
<class>gotware.finance.curves.CurvePoint</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>

<properties>

<property name="eclipselink.logging.thread" value="true" />
<property name="eclipselink.logging.timestamp" value="true" />
<property name="eclipselink.logging.exceptions" value="true" />
<property name="eclipselink.logging.connection" value="true" />
<property name="eclipselink.logging.level" value="FINEST" />
</properties>
</persistence-unit>
</persistence>


package gotware.finance.currency;

import gotware.finance.curves.Curve;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
* Entity implementation class for Entity: CurrencyArea
*
*/
@Entity

public class Currency implements Serializable {
@OneToMany(mappedBy="currency",fetch=FetchType.EAGER,cascade=CascadeType.ALL)
List<Curve> curves;
private static final long serialVersionUID = 1L;
@Id
String currencyName;

/**
* @param currencyName
*/
public Currency(String currencyName) {
super();
this.currencyName = currencyName;
}


public String getCurrencyName() {
return currencyName;
}


public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}


public Currency() {
super();
}


public List<Curve> getCurves() {
return curves;
}
public void setCurves(List<Curve> curves) {
this.curves = curves;


}



}

package gotware.finance.curves;

import gotware.dataobject.GotwareDataObject;
import gotware.finance.base.Security;
import gotware.finance.currency.Currency;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

/**
* Entity implementation class for Entity: Curve
*
*/
@Entity
public class Curve implements Serializable {

@Id
private String curveName;
private String description;
private static final long serialVersionUID = 1L;
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
private Currency currency;
@ManyToMany(mappedBy = "associatedCurves",fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Security> securityList = new ArrayList<Security>();
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List<CurvePoint> curvePoints;
@Transient
private GotwareDataObject gotwareDataObject;


public Curve(){
super();
}
public List<CurvePoint> getCurvePoints() {
return curvePoints;
}
public void setCurvePoints(List<CurvePoint> curvePoints) {
this.curvePoints = curvePoints;
}
public Curve(List<Security> securityList) {
super();
this.securityList=securityList;

}



public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
public List<Security> getSecurityList() {
return securityList;
}
public void setSecurityList(List<Security> securityList) {
this.securityList = securityList;
}


public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}

public GotwareDataObject getGotwareDataObject() {
return gotwareDataObject;
}
public void setGotwareDataObject(GotwareDataObject gotwareDataObject) {
this.gotwareDataObject = gotwareDataObject;
}

public List<String> getLabels(){
// TODO This method should be overwritten by special implementation of the curve class to provide the label of the x axis of the curve
List<String> labels = new ArrayList<String>();
Iterator<Security> securityIterator = securityList.iterator();
while (securityIterator.hasNext()) {
Security security = (Security) securityIterator.next();
labels.add(security.getName());
}
return labels;
}



public void update(Security security){
recomputeNewCurve();
if(gotwareDataObject!=null){
gotwareDataObject.registerForUpdate(this);
}
}

public void recomputeNewCurve(){
// TODO: to be ovverridden in specific curves implementation updating values array
// This should modify curvePoints array
Iterator<Security> iterSec = securityList.iterator();
// TODO : remove this sample code
for(int i=0;i<securityList.size();i++){
Security security = securityList.get(i);
if(curvePoints.size()>i){
CurvePoint point = curvePoints.get(i);
point.setValue(security.getMarketData().getLastPrice());
}
}
}
public void setCurveName(String curveName) {
this.curveName = curveName;
}
public String getCurveName() {
return curveName;
}



}


I have also added a set of CurvePoints to the Curve, and I occurr in the same problems. I persist them and they are fine n the database, but when I load with the em.find the entity "loses" this fields :(Sad

Thank you


Re: EclipseLink 2.0 ManyToOne not working correctly [message #637183 is a reply to message #636193] Thu, 04 November 2010 13:53 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

A few issues I noticed in the code,

EoniaSwap eonia2 = new EoniaSwap();
eonia2.setName("Eonia 2 week swap");
eonia2.setDescription("Daily fixing compounded over period stipulated");
eonia2.setTicker("EUSWE2Z Curncy");
BigDecimal bigDecimal2= new BigDecimal("0.30");
****eonia1.getMarketData().setLastPrice(bigDecimal2);
>> should be eonia2
securityList.add(eonia2);
em.persist(eonia2);

EoniaSwap eonia3 = new EoniaSwap();
eonia3.setName("Eonia 3 week swap");
eonia3.setDescription("Daily fixing compounded over period stipulated");
eonia3.setTicker("EUSWE3Z Curncy");
BigDecimal bigDecimal3= new BigDecimal("0.35");
****eonia1.getMarketData().setLastPrice(bigDecimal3);
>> should be eonia2
securityList.add(eonia3);
em.persist(eonia3);
em.flush();


Curve eoniaCurve = new Curve(securityList);
eoniaCurve.setCurveName("Pippo pluto");
eoniaCurve.setCurrency(currency);
CurvePoint curvePoint1 = new CurvePoint("GOTX1234",new BigDecimal(0.73));
CurvePoint curvePoint2 = new CurvePoint("GOTX1235",new BigDecimal(0.75));
CurvePoint curvePoint3 = new CurvePoint("GOTX1236",new BigDecimal(0.77));
List<CurvePoint> curvePoints = new ArrayList<CurvePoint>();
curvePoints.add(curvePoint1);
curvePoints.add(curvePoint2);
curvePoints.add(curvePoint3);
curvePoint1.setCurve(eoniaCurve);
curvePoint2.setCurve(eoniaCurve);
curvePoint3.setCurve(eoniaCurve);

List<Curve> curves = new ArrayList<Curve>();
curves.add(eoniaCurve);
***eonia1.setAssociatedCurves(curves);
***eonia2.setAssociatedCurves(curves);
***eonia3.setAssociatedCurves(curves);
>> you cannot share the same collection with multiple different objects, each must have its own collections, i.e.
eonia3.setAssociatedCurves(new ArrayList<Curve>(curves);

If still having the issue, what is you remove the flush, or call refresh, what happens?


James : Wiki : Book : Blog : Twitter
Previous Topic:Weaving and OSGi
Next Topic:Postgis Eclipselink JPA
Goto Forum:
  


Current Time: Sat Dec 20 12:15:27 GMT 2014

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

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