com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“字段列表”中的“CONTACTID”列未知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23947197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'CONTACTID' in 'field list'
提问by topcan5
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.
我使用 EclipseLink 从 MySQL 生成我的所有对象,我有一个名为CONTACT_ID
. 当我尝试时em.persist(contact)
,我收到一条错误消息,上面写着“ Unknown column 'CONTACTID'
”。
我搜索了我的项目和数据库,我没有任何名为CONTACTID
. 不知何故,JPA 删除了此列名称中的“_”。我确信这真的很愚蠢,我就是不知道为什么。
Here is my db design:
这是我的数据库设计:
User.java:
用户.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:
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:
用户PK.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;
采纳答案by Chris
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.
确保您的所有注释在整个模型中都是一致的。
回答by saneryee
One Reason is that your column name is wrong in your entity code. Check it with your database column name.
一个原因是您的实体代码中的列名是错误的。用你的数据库列名检查它。
回答by Damir Olejar
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.
由于我使用 mysql 的 jdbc 连接器与 JPA(Java 忍者框架)连接,重新启动服务器,清理和重建代码,删除和输入列工作......我知道点击重置按钮并不是真的一个有效的答案,但如果有人像我一样绝望地修复它,这篇文章可能会有所帮助。
回答by JamesENL
Three possible explanations;
三种可能的解释;
- 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).
- As you say, eclipselink is ignoring the column value.
- 您正在使用 SQL 脚本生成数据库架构,但列名不匹配(不太可能,因为您使用的是 JPA 注释)。
- 正如您所说,eclipselink 忽略了列值。
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)
你可以做的事情。启用您的日志记录级别以显示创建表时正在使用的确切 SQL。逐步调试调试器(我从未使用过 eclipselink,所以我不知道这是否可行)。其他有帮助的事情是发布您的 Spring 配置,以便我们可以看到您如何配置应用程序上下文以及堆栈跟踪(尽管它不太可能有帮助,因为您的错误非常具体)