0

I have a many-to-many relationship which looks like this: enter image description here

The primary key is combination of three columns and I'm using eclipselink. I created these classes to be able to insert in the join-table :

@Entity
@Table(name = "definition_property")
@NamedQueries({
    @NamedQuery(name = "DefinitionProperty.findAll", query = "SELECT d FROM DefinitionProperty d")})
public class DefinitionProperty extends AbstractEntity{
    private static final long serialVersionUID = 1L;

     @EmbeddedId
     protected DefinitionPropertyPK pk;

    @JoinColumn(name = "dtid", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private DefinitionType definitionType;
    @JoinColumn(name = "prid", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Property property;
    @Column(name = "initial_value")
    @Basic(optional = false)
    private String initialValue;


    // setters and getters
}

And PK class:

@Embeddable
public class DefinitionPropertyPK implements Serializable{


    @Column(name = "dtid")
    private Integer idDt;

    @Column(name = "prid")
    private Integer idProperty;

    @Column(name = "initial_value")
    private String initialValue;

    //hashcode, equals, setters and getters
}

Entitiy 1:

@Entity
@Table(name = "definition_type")

public class DefinitionType extends AbstractEntity implements EntityItem<Integer> {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer idDT;
    @Size(max = 45)
    @Column(name = "name")
    private String dtName;
       @Size(max = 45)
    @Column(name = "description")
    private String description;

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "definitionType")
    private List<DefinitionProperty> definitionProperties = new ArrayList<DefinitionProperty>();
}

Entity 2:

@Entity
@Table(name = "property")

public class Property extends AbstractEntity implements EntityItem<Integer>{

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @Basic(optional = false )
    @Column(name = "id")
    private Integer idProperty;
@Column(name = "name")
    @Size(max = 45)
    private String propertyName;
    @Enumerated(EnumType.STRING)
    @Column(name = "type")
    private Type fieldType;
@Column(name = "init_value")
    @Size(max = 45)
    private String initialValue;

    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "property")  
    private List<DefinitionProperty> definitionPeoperties= new ArrayList<DefinitionProperty>();

}

Exception : I get this exception when trying to persist a new DefinitionType:

   Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The column name 'initial_value' is specified more than once in the SET clause. A column cannot be assigned more than one value in the same SET clause. Modify the SET clause to make sure that a column is updated only once. If the SET clause updates columns of a view, then the column name 'initial_value' may appear twice in the view definition.
Error Code: 264
Call: INSERT INTO definition_property (initial_value, initial_value, dtid, prid) VALUES (?, ?, ?, ?)
    bind => [4 parameters bound]

Question : Why there are two initial_values in the set clause and where am I wrong in my code?

Ref: How do you Set Up a Many-to-Many Relationship with Junction Table using JPA/EclipseLink

Ref: JPA simple many-to-many with eclipselink

Ref: https://giannigar.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/

Community
  • 1
  • 1
gabi
  • 1,324
  • 4
  • 22
  • 47
  • 1
    Try adding `insertable = false, updateable = false` to `DefinitionPropertyPK#initialValue` – Predrag Maric Dec 02 '14 at 09:14
  • Thanks for your answer predrag, that solved the first exception but now I have another exception, when I persist I get "Cannot insert the value NULL into column". it seems that all parameters in DefinitionProperty are null. And I'm not sure about my service layers code as well. How should I ask this? should I add it to the question or ask a new question? – gabi Dec 02 '14 at 10:32
  • Are all parameters null, or just `initialValue`? Also, is it necessary for `initialValue` to be part of primary key? Looks like other two columns should be enough. And, you should probably ask a new question, this one is already pretty big. – Predrag Maric Dec 02 '14 at 10:37
  • in the exception only initialValue is mentioned which cannot be null in this level. in my service I have set all parameters and nothing is null. and yes they all need to be part of key because I can have same definition and property with different initial values – gabi Dec 02 '14 at 10:41
  • What if you move `insertable = false, updateable = false` from `DefinitionPropertyPK#initialValue` to `DefinitionProperty#initialValue`? – Predrag Maric Dec 02 '14 at 10:50
  • 1
    no I have already tried that. Maybe its better to ask a new question and describe it there. Its a bit unclear here. – gabi Dec 02 '14 at 11:26
  • THe problem is your application is not setting the writable attributes in your entity model that control the database field values. With the updated class, you set the fields within the entity to be read-only, which means you must set the values within the embeddable for them to be inserted/updated in the database. This is why it is usually a bad idea to have multiple mappings for the same field, as the application is required to keep both insync - JPA does not do it for you. – Chris Dec 02 '14 at 15:24
  • its true chris, I already solved it with removing insertable and updatable properties here: http://stackoverflow.com/questions/27248673/jpa-many-to-many-with-additional-column/27248872?noredirect=1#comment42972287_27248872 – gabi Dec 02 '14 at 15:32

0 Answers0