我有多对多的关系,看起来像这样:
主键是三列的组合,我正在使用eclipselink。我创建了这些类,以便能够在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
}
PK课程:
@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
}
实体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>();
}
实体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>();
}
异常:尝试保留新的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]
问题:为什么set子句中有两个initial_values,我的代码中哪里出错了?
参考:How do you Set Up a Many-to-Many Relationship with Junction Table using JPA/EclipseLink