eclipselink SQLServerException inserting into a join table with additional column

限于喜欢 提交于 2019-12-11 23:05:40

问题


I have a many-to-many relationship which looks like this:

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/

来源:https://stackoverflow.com/questions/27245174/eclipselink-sqlserverexception-inserting-into-a-join-table-with-additional-colum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!