views:

513

answers:

5

Hi All,

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!

A: 

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.

Thilo
A: 

Hi, it's me who asked to question - I tried what you suggested but it didn't help me... it still tries to insert the first name into the table. Thanks.

WhiteTigerK
move this to a comment please (if you have the rep)
Thilo
A: 

set field's default value in your bean's empty constructor.

    @Basic(optional=false)
    @Column(nullable=false)
    private String firstName;

    public MyBean()
    {
      this.firstName = "defaultValue";
    }
Elvis D.
+1  A: 

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)
Pascal Thivent
A: 

Hi, 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{


}
funfin