【发布时间】:2011-07-15 09:25:32
【问题描述】:
我有一个实体类,其中包含位于不同表中的键值对映射,并且给定实体可能没有这样的对。实体类的相关代码如下。
现在,当我使用 persist() 插入这样的实体,然后添加键值对,然后使用 merge() 保存它时,存储键值对的相关表出现重复输入错误。我试图阻止插入,直到添加键,只调用一次persist()。这导致在外键列 (ixSource) 中包含空(零)id 的重复输入错误。
我在调试器中跟踪处理,发现eclipselink似乎对级联感到困惑。在更新实体时,它会执行更新相关表的调用。尽管如此,它也会将这些操作添加到随后处理的队列中,这是发生重复条目错误的时候。我试过CascadeType.ALL和MERGE,没有区别。
如果重要的话,我正在使用静态编织。
这是实体的代码,为简洁起见:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "sType")
@Table(name = "BaseEntity")
public abstract class BaseEntity extends AbstractModel
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ix")
private long _ix;
}
@Entity
@Table(name = "Source")
public class Source extends BaseEntity
{
@OneToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "SourceProperty", joinColumns = { @JoinColumn(name = "ixSource") })
@MapKey(name = "sKey")
private Map<String, SourceProperty> _mpKeys;
// ... there's more columns that probably don't matter ...
}
@Entity
@Table(name = "SourceProperty")
@IdClass(SourcePropertyKey.class)
public class SourceProperty
{
@Id
@Column(name = "sKey", nullable = false)
public String sKey;
@Id
@Column(name = "ixSource", nullable = false)
public long ixSource;
@Column(name = "sValue", nullable = true)
public String sValue;
}
public class SourcePropertyKey implements Serializable
{
private final static long serialVersionUID = 1L;
public String sKey;
public long ixSource;
@Override
public boolean equals(Object obj)
{
if (obj instanceof SourcePropertyKey) {
return this.sKey.equals(((SourcePropertyKey) obj).sKey)
&& this.ixSource == ((SourcePropertyKey) obj).ixSource;
} else {
return false;
}
}
}
【问题讨论】:
标签: jpa eclipselink