Bhakti,
The simplest solution/workaround would be to add the following to
your default constructor for Employee:
public Employee() {
address1 = new Location();
address2 = new Location();
}
Since it appears the null embedded is causing the COUNTRY_CODE
field to be nulled out on write out. You may want to enter a bug
to have someone look at that further.
Your issue #3 listed below looks like a bug on our part in that
we're not respecting the override setting, please enter a bug. You
could get around this issue in the mean time by adding a session
event listener (eclipselink.session-event-listener) and updating
the mappings (address1 and address2) from AnotherEmployee.Location
to be writeable (mapping.setIsReadOnly(false)) in the postLogin
event.
Cheers,
Guy
On 30/10/2012 3:17 AM, bhakti b wrote:
Thanks Guy for the reply, but it doesn't solve the issue I am facing,
1. Firstly since the column name could be different in the tables used by
the parent entity using the embedded attribute, I cannot set the column name
in the embeddable class.
2. Secondly when I am using the same column to hold the country code, null
gets inserted in the column if one of the embedded attributes is not set.
For instance consider the foll example :
<entity class="com.model.Employee">
<attributes>
<id name="id">
<column name="EMP_ID" />
<generated-value />
</id>
<basic name="name">
<column name="EMP_NAME" />
</basic>
<basic name="country" attribute-type="java.lang.String">
<column name="COUNTRY_CODE" insertable="true" updatable="true" />
</basic>
<embedded attribute-type="com.model.Location" name="address1">
<attribute-override name="address">
<column name="ADDR1" />
</attribute-override>
<attribute-override name="countryCode">
<column name="COUNTRY_CODE" />
</attribute-override>
</embedded>
<embedded attribute-type="com.model.Location" name="address2">
<attribute-override name="address">
<column name="ADDR2" />
</attribute-override>
<attribute-override name="countryCode">
<column name="COUNTRY_CODE" />
</attribute-override>
</embedded>
</attributes>
</entity>
<embeddable class="com.model.Location">
<attributes>
<basic name="address" attribute-type="java.lang.String" />
<basic name="countryCode" attribute-type="java.lang.String">
<column insertable="false" updatable="false" />
</basic>
</attributes>
</embeddable>
My Employee.java looks like this :
public class Employee implements Serializable{
private int id;
private String name;
private String country;
private Location address1;
private Location address2;
...//constructors, setters and getters
}
Now when the Employee object is persisted:
Case 1 : When I set the country attribute and set both the address
attributes, it works fine.
//This Works
Employee e = new Employee();
e.setName("SomeName");
e.setCountry("AUS");
e.setAddress1(new Location("Home2", "AUS"));
e.setAddress2(new Location("Work2", "AUS"));
[EL Fine]: sql: 2012-10-30
12:22:06.177--ClientSession(1377542617)--Connection(2134123845)--Thread(Thread[main,5,main])--INSERT
INTO JPA_EMPLOYEE (EMP_ID, COUNTRY_CODE, EMP_NAME, ADDR1, ADDR2) VALUES (?,
?, ?, ?, ?)
bind => [1, AUS, SomeName, Home2, Work2]
Case 2 : When I set country attribute and set ONLY one embeddable
attribute, it inserts null. Address2 is not set here
//This doesn't work !
Employee e = new Employee();
e.setName("SomeName");
e.setCountry("AUS");
e.setAddress1(new Location("Home2", "AUS"));
[EL Fine]: sql: 2012-10-30
12:24:46.277--ClientSession(1407621005)--Connection(386934670)--Thread(Thread[main,5,main])--INSERT
INTO JPA_EMPLOYEE (EMP_ID, COUNTRY_CODE, EMP_NAME, ADDR1, ADDR2) VALUES (?,
?, ?, ?, ?)
bind => [1, null, SomeName, Home2, null]
3. One more issue that i am facing : Since the embeddable class is shared
across multiple entities, another entity could have attribute overrides
using different columns and want to persist using the embeddable class
itself. Here however the insertable and updatable properties of the
attribute column are not getting overriden.
i.e. consider the same embeddable class given above, and the following
entity, explicit country column is not present in this entity.
<entity class="com.model.AnotherEmployee">
<attributes>
<id name="id">
<column name="EMP_ID" />
<generated-value />
</id>
<basic name="name">
<column name="EMP_NAME" />
</basic>
<embedded attribute-type="com.model.Location" name="address1">
<attribute-override name="address">
<column name="ADDR1" />
</attribute-override>
<attribute-override name="countryCode">
<column name="ADDR1_COUNTRY_CODE" insertable="true" updatable="true"/>
</attribute-override>
</embedded>
<embedded attribute-type="com.model.Location" name="address2">
<attribute-override name="address">
<column name="ADDR2" />
</attribute-override>
<attribute-override name="countryCode">
<column name="ADDR2_COUNTRY_CODE" insertable="true" updatable="true"/>
</attribute-override>
</embedded>
</attributes>
</entity>
The Entity looks like :
public class AnotherEmployee implements Serializable {
private int id;
private String name;
private Location address1;
private Location address2;
...
}
Now when the entity AnotherEmployee is persisted :
AnotherEmployee ae = new AnotherEmployee();
ae.setName("SomeOtherName");
ae.setAddress1(new Location("Work1", "AUS"));
ae.setAddress2(new Location("Work2", "AUS"));
The country code columns are not populated, as the overridden
insertable=true and updatable=true is not picked up.
The sql log:
[EL Fine]: sql: 2012-10-30
12:36:32.825--ClientSession(36987345)--Connection(630515574)--Thread(Thread[main,5,main])--INSERT
INTO JPA_ANOTHER_EMPLOYEE (EMP_ID, EMP_NAME, ADDR1, ADDR2) VALUES (?, ?, ?,
?)
bind => [2, SomeOtherName, Work1, Work2]
Both the examples mentioned above are valid scenarios in our application.
Can someone suggest a way to achieve the above functionality ?
--
View this message in context: http://eclipse.1072660.n5.nabble.com/Is-there-a-way-by-which-we-can-map-the-same-column-to-multipe-embedded-attribute-overrides-tp155354p155388.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
--

Guy Pelletier
ORACLE Canada,
45 O'Connor Street
Suite 400
Ottawa, Ontario
Canada K1P 1A4
Oracle is committed to developing
practices and products that help protect the environment
|