EJB3 - @Column(insertable=“false”) 问题

发布于 2024-08-20 02:40:01 字数 1007 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞旧 2024-08-27 02:40:01

使用以下注释:

@Basic(optional = false)
@Column(name = "FIRST_NAME", insertable = false, updatable = true, nullable = false)
private String m_firstName;

在生成 SQL 插入时,FIRST_NAME 列应该绝对被忽略。换句话说,这听起来像是 GlassFish 社区开发的 TopLink Essentials 中的一个错误。实际上,我认为这个问题在 Issue #627 中报告(“列注释 insertable=false 仅在与 updatable=false 一起使用时才有效”)。遗憾的是,它没有修复,所以我建议:

  • 使用 private String m_firstName = "my database default"; (是的,这很难看) - 或 -
  • 更改持久性提供程序(对于 OpenJPA,冬眠)

With the following annotations:

@Basic(optional = false)
@Column(name = "FIRST_NAME", insertable = false, updatable = true, nullable = false)
private String m_firstName;

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:

  • to use private String m_firstName = "my database default"; (yes, this is ugly) - OR -
  • to change the persistence provider (for OpenJPA, Hibenate)
北城挽邺 2024-08-27 02:40:01

尝试将这样的东西添加到您的实体类中。
这意味着在 Hibernate 的 SQL 中排除 null 属性值

@Entity
@Table(name = "your_table")
@org.hibernate.annotations.Entity(
  dynamicInsert = true
)
public class YourClass{


}

try add somthing like this to your entity class.
This means exclude null property values in the Hibernate’s SQL

@Entity
@Table(name = "your_table")
@org.hibernate.annotations.Entity(
  dynamicInsert = true
)
public class YourClass{


}
万人眼中万个我 2024-08-27 02:40:01

仅当您同时设置 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/

战皆罪 2024-08-27 02:40:01

您可以尝试不使用 @Basic(Optional = true) 吗,因为 映射NULLABLE(在你的情况下这似乎是错误的)?

定义字段或属性的值是否可以为空。

Can you try without @Basic(optional = true), because that maps to NULLABLE (which seems to be wrong in your case)?

Defines whether the value of the field or property may be null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文