EJB3 - @Column(insertable=“false”) 问题
我正在使用 EJB3 和 Oracle Express Edition DB 构建 J2SE 应用程序。
我的问题是这样的 - 我在项目中设置了一个与数据库中的表匹配的 EntityBean。该表包含一个不可为空且具有默认值的列。我想要的只是当使用 EJB 将新数据保存到该表时,该列的值将获得其默认值。这就是我在项目中设置它的方式:
//holds user's first name
@Basic(optional = true)
@Column(name = "FIRST_NAME", insertable = false, updatable = true, nullable = false)
private String m_firstName;
我也在 ORM.XML 文件中设置它:
<basic name="firstName">
<column name="FIRST_NAME" insertable="false" updatable="true" nullable="false"/>
</basic>
但由于某种原因,当创建一个新的 EntityBean 并且不设置名字字段,然后尝试保留它时,我得到以下内容例外:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-01400: cannot insert NULL into ("TSDB"."USERS"."FIRST_NAME")
这意味着持久性管理器尝试插入名字字段,尽管我告诉它不要这样做。
我在这里做错了什么吗?
谢谢!
I'm building a J2SE application with EJB3 and an Oracle Express Edition DB.
My problem is like that - I set an EntityBean in my project which matches a table in the DB. The table contains a column which is not nullable and has a default value. All I want is that when persisting a new data to this table using the EJB, the column's value will get its default value. This is how I set it in the project:
//holds user's first name
@Basic(optional = true)
@Column(name = "FIRST_NAME", insertable = false, updatable = true, nullable = false)
private String m_firstName;
I also set it in the ORM.XML file:
<basic name="firstName">
<column name="FIRST_NAME" insertable="false" updatable="true" nullable="false"/>
</basic>
But for some reason, when creating a new EntityBean and not setting the first name field, and then trying to persist it, i get the following exception:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-01400: cannot insert NULL into ("TSDB"."USERS"."FIRST_NAME")
Which means that the persistence manager tries to insert the first name field although I told it not to.
Am I doing something wrong here ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用以下注释:
在生成 SQL 插入时,
FIRST_NAME
列应该绝对被忽略。换句话说,这听起来像是 GlassFish 社区开发的 TopLink Essentials 中的一个错误。实际上,我认为这个问题在 Issue #627 中报告(“列注释 insertable=false 仅在与 updatable=false 一起使用时才有效”)。遗憾的是,它没有修复,所以我建议:private String m_firstName = "my database default";
(是的,这很难看) - 或 -With the following annotations:
The
FIRST_NAME
column should definitely be ignored when generating SQL inserts. In other words, this sounds like a bug in TopLink Essentials which is developed in the GlassFish community. Actually, I think this issue is reported in Issue #627 ("Column annotation insertable=false only works when used with updatable=false"). Sadly, it is not fixed so I'd suggest:private String m_firstName = "my database default";
(yes, this is ugly) - OR -尝试将这样的东西添加到您的实体类中。
这意味着在 Hibernate 的 SQL 中排除 null 属性值
try add somthing like this to your entity class.
This means exclude null property values in the Hibernate’s SQL
仅当您同时设置 insertable 和 updateable = false 时,TopLink Essentials 才会忽略该列。
该问题已在 EclipseLink 中修复,因此您应该考虑升级。
http://www.eclipse.org/eclipselink/
TopLink Essentials will only omit the column if you have both insertable and updateable = false.
This was fixed in EclipseLink, so you should consider upgrading.
http://www.eclipse.org/eclipselink/
您可以尝试不使用
@Basic(Optional = true)
吗,因为 映射到NULLABLE
(在你的情况下这似乎是错误的)?Can you try without
@Basic(optional = true)
, because that maps toNULLABLE
(which seems to be wrong in your case)?