6

I use EclipseLink to generate all my objects from MySQL, I have a field called CONTACT_ID. When I am trying to em.persist(contact), I got an errer msg saying "Unknown column 'CONTACTID'".
I searched through out my project and database, I don't have any column named CONTACTID. Somehow JPA removes the '_' in this column name. I am sure it's something really stupid, I just can't find out why.

Here is my db design: enter image description here

User.java:

@EmbeddedId
private UserPK id;

private int score;

//bi-directional many-to-one association to CmnContact
@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn (name="CONTACT_ID")
private CmnContact cmnContact;

//bi-directional many-to-one association to Login
@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn (name="LOGIN_ID")
private Login login;

CmnContact.java:

@Id
@JoinColumn(name="CONTACT_ID")
private String contactId;
//bi-directional many-to-one association to User
@OneToMany(mappedBy="cmnContact")
private List<User> users;

UserPK.java:

@Column(name="USER_ID")
private UUID userId;

@Column(name="CONTACT_ID", insertable=false, updatable=false)
private UUID contactId;

@Column(name="LOGIN_ID", insertable=false, updatable=false)
private UUID loginId;
topcan5
  • 1,511
  • 8
  • 30
  • 54
  • 1
    It looks like EclipseLink ignores your JPA field annotation entirely - it ignores not only `name="CONTACT_ID"` but `insertable=false` also, the latter means this field shouldn't be used at `persist`... Do you have other JPA annotations in your code? Are they work as expected? – dened May 30 '14 at 06:12

4 Answers4

3

This usually occurs when you have mixed access type - having some annotations on properties and some on fields. Unless you override the access type, the provider is left to pick the access type for the entity based on what annotation it finds first, and so will ignore others. A good explaination of the access type is given here

Make sure that all your annotations are consistent through out the model.

Community
  • 1
  • 1
Chris
  • 20,138
  • 2
  • 29
  • 43
  • Chris, I just uploaded my db design and java code auto generated by EclipseLink. Can you take a look and tell me the problem? Thank you! – topcan5 Jun 03 '14 at 04:56
  • use Column(name="CONTACT_ID") on the private String contactId; instead of JoinColumn(name="CONTACT_ID"). – Chris Jun 03 '14 at 16:24
  • Thank you Chris! It fixed my problem. Just for my info, why does EclispeLink use JoinColumn instead of Column? Thanks again! – topcan5 Jun 04 '14 at 02:23
  • JoinColumns are used to specify both the column in the local table and the column it references in a target table - used for relationship mappings. The column annotation is used to specify only the column in the local table, used for basic mappings. Because it was a basic mapping, the joincolumn annotation was ignored and should have been showing a warning if the logging was turned up. – Chris Jun 04 '14 at 12:53
0

Three possible explanations;

  1. You are using an SQL script to generate your database schema and your column names don't match (unlikely as you are using the JPA annotations).
  2. As you say, eclipselink is ignoring the column value.

Things you can do. Enable your logging level to show the exact SQL that is being used when creating your tables. Step through the debugger (i've never used eclipselink, so I don't know if this is viable). Other things that would help would be to post your Spring config so we can see how you are configuring your application context as well as the stacktrace (even though its pretty unlikely that it will help, as your error is pretty specific)

JamesENL
  • 6,400
  • 6
  • 39
  • 64
0

One Reason is that your column name is wrong in your entity code. Check it with your database column name.

saneryee
  • 3,239
  • 31
  • 22
0

Since I am using the jdbc connector for mysql to connect with JPA (ninja framework for Java), restarting the server, cleaning and rebuilding the code, removing and entering the column works... I am aware that hitting the reset button is not really a valid answer, but if anyone is as desperate as I were to fix it, this post may help.

3xCh1_23
  • 1,491
  • 1
  • 20
  • 39