[英]EJB3 - @Column(insertable=“false”) question
I'm building a J2SE application with EJB3 and an Oracle Express Edition DB. 我正在使用EJB3和Oracle Express Edition DB构建J2SE应用程序。
My problem is like that - I set an EntityBean in my project which matches a table in the DB. 我的问题就是这样 - 我在项目中设置了一个与数据库中的表匹配的EntityBean。 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. 我想要的是,当使用EJB将新数据持久化到此表时,列的值将获得其默认值。 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: 我还在ORM.XML文件中设置它:
<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: 但由于某种原因,在创建新的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")
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! 谢谢!
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. 生成SQL插入时, 绝对应该忽略FIRST_NAME
列。 In other words, this sounds like a bug in TopLink Essentials which is developed in the GlassFish community. 换句话说,这听起来像是在GlassFish社区中开发的TopLink Essentials中的一个错误。 Actually, I think this issue is reported in Issue #627 ( "Column annotation insertable=false only works when used with updatable=false" ). 实际上,我认为问题是在问题#627中报告的( “Column annotation insertable = false仅在与updatable = false一起使用时才起作用” )。 Sadly, it is not fixed so I'd suggest: 可悲的是,它没有固定,所以我建议:
private String m_firstName = "my database default";
使用private String m_firstName = "my database default";
(yes, this is ugly) - OR - (是的,这很难看) - 或者 - try add somthing like this to your entity class. 尝试将这样的东西添加到您的实体类。 This means exclude null property values in the Hibernate's SQL 这意味着在Hibernate的SQL中排除null属性值
@Entity
@Table(name = "your_table")
@org.hibernate.annotations.Entity(
dynamicInsert = true
)
public class YourClass{
}
TopLink Essentials will only omit the column if you have both insertable and updateable = false. 如果您同时具有insertable和updateable = false,则TopLink Essentials将仅省略该列。
This was fixed in EclipseLink, so you should consider upgrading. 这已在EclipseLink中修复,因此您应该考虑升级。
http://www.eclipse.org/eclipselink/ http://www.eclipse.org/eclipselink/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.