开发者

EJB3 - @Column(insertable="false") question

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 开发者_如何学JAVAname 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. 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)


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{


}


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/


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜