Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Error in Eclipselink with @ElementCollectionMap and @JoinColumn

You currently can not use a non-id field within the join columns of your element collection.

Note: This is an optional component of the spec (section 10.1.21):

"Support for referenced columns that are not primary key columns of the referenced table is optional. Applications that use such mappings will not be portable."

Feel free to log an enhancement request.

Cheers,
Guy

----- Original Message ----- From: "vide" <vidas.timukas@xxxxxxxxx>
To: <eclipselink-users@xxxxxxxxxxx>
Sent: Wednesday, March 10, 2010 8:22 AM
Subject: [eclipselink-users] Error in Eclipselink with @ElementCollectionMap and @JoinColumn



Hello,

I stuck with problem when I try to use @ElementCollection join to Map with
@JoinColumn, which is not a primary key. When I try to update a Map,
JoinColumn value becomes null and Eclipselink return with error.

My entity:
---------------- @Entity
@Table(name = "cust")
public class Customer implements Serializable {
   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id")
   private Long id;
   @Column(name = "name")
   private String name;
   @Column(name = "address", nullable = false)
   private String address;

   @ElementCollection
   @CollectionTable(name = "address_info",
       joinColumns = {
           @JoinColumn(name = "x_address", referencedColumnName =
"address")
   })
   @MapKeyColumn(name = "info_tag")
   @Column(name = "info_value")
   private Map<String, String> extInfo;

   public Customer() {
       extInfo = new HashMap<String, String>();
   }

   public Customer(String name, String address) {
       this();
       this.name = name;
       this.address = address;
   }

   ...Getters and setters
}

Code:
------------- Customer cust = new Customer("My Customer", "Arctic");
em.persist(cust);
em.flush();
em.refresh(cust);
cust.setInfo("postIndex", "12345");

Error log:
------------- FINE: INSERT INTO cust (address, name) VALUES (?, ?)
       bind => [Arctic, My Customer]
FINE: SELECT LAST_INSERT_ID()
FINE: SELECT id, address, name FROM cust WHERE (id = ?)
       bind => [1]
FINE: SELECT t0.info_value, t0.info_tag FROM address_info t0 WHERE
(t0.x_address = ?)
       bind => [Arctic]
FINE: INSERT INTO address_info (x_address, info_value, info_tag) VALUES (?,
?, ?)
       bind => [null, 12345, postIndex]
FINE: SELECT 1
WARNING: Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services -
2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Column 'x_address' cannot be null
Error Code: 1048
Call: INSERT INTO address_info (x_address, info_value, info_tag) VALUES (?,
?, ?)
       bind => [null, 12345, postIndex]
Query: DataModifyQuery(sql="INSERT INTO address_info (x_address, info_value,
info_tag) VALUES (?, ?, ?)")


When I remove em.flush() and em.refresh(cust), everything works fine, until
next transaction, when I try to find inserted entity and make an update to
Map, error returns. If I change JoinColumn to use entity's ID, problem
disappears. Why I can't use JoinColumn, different from ID column?

Thanks

--
View this message in context: http://old.nabble.com/Error-in-Eclipselink-with-%40ElementCollection-Map-and-%40JoinColumn-tp27849535p27849535.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




Back to the top