users@glassfish.java.net

Problem defining insertable=false at column in a embedded class

From: <glassfish_at_javadesktop.org>
Date: Mon, 30 Apr 2007 04:38:53 PDT

Hi
I have an embedded class (ClassPK) to define the primary key for my entity bean (ClassBean). But one of his fields is a foreign key for another entity bean (AnotherBean) and so this field must be populated by the relationship that exist in the entity (ClassBean) with the annotation @joincolumn.

I think the idea is correct. Although I have a problem with the population of this field. Because both the association and the field it self tries to write in DB. The solution would be to add in the @column annotation the argument insertable=false. So that just the association tries to populate the field.

But this seems not to work. the "Multiple writable mappings exist for the field" exception is thrown.

If I try to put the insertable=false at the association instead of the field. A null value is populated.

This is the code:

[code]
@Entity
@Table(name="AnotherBeanTable")
public class AnotherBean implements Serializable{

    @Id
    @Column(name = "FIELD_NB", nullable=false)
    protected BigInteger fieldNb;

    /*
        getters and setters and others methods
    */
}


@Entity
@Table(name="ClassBeanTable")
public class ClassBean implements Serializable{
    /**
     * EmbeddedId primary key field
     */
    @EmbeddedId
    protected ClassPK classPk;

    @JoinColumn(name = "FIELD_NB", referencedColumnName = "FIELD_NB")
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.ALL})
    private AnotherBean anotherBean;

    /*
        getters and setters and others methods
    */
}


@Embedded
public class ClassPK implements Serializable{

    @Column(name = "FIELD_NB", nullable = false,
                insertable = false, updatable = false)
    protected BigInteger fieldNb;

    @Column(name = "TEXT_FIELD", nullable = false)
    private String textField;

    /*
        getters and setters and others methods
    */
}

[/code]


Is there another way to do this? Or is there any mistakes?

I believe that its possible to do this with IdClass, but I would like to use an embedded class. And I think this should be possible
[Message sent by forum member 'dnlcy' (dnlcy)]

http://forums.java.net/jive/thread.jspa?messageID=214879