HY!
这让我疯狂了几天......希望有人可以帮助我。
情况:
我有一个简单的 datamodel ,有三个表:
联接表(CompProd)有其他列。
我的(已剥离)实体如下:
@Entity
@Table(name = "sa_s_comp")
public class SComp implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int compId;
@Column(unique = true, nullable = false, length = 50)
private String compName;
@Column(length = 50)
private String compDesc;
@Column(nullable = false)
private Boolean compIsOnAvailability;
@Min(value = 0)
@Column(nullable = false)
private int compIndex;
@Column(nullable = false)
private Lkzs lkz;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date cdate;
@Column(nullable = false)
private String cuser;
@NotNull
@OneToOne(optional = false)
@JoinColumn(name = "compgroupId", nullable = false)
private SCompGroup group;
@OneToMany(mappedBy = "component", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<LCompProd> products;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "transId", unique = true, nullable = true)
private STrans translation;
...
/**
* Adds the given product to the list of assigned products for this
* component.
*
* @param prod
* the product to be assigned
* @return the component
*/
public void addProduct(SProd prod) {
if (this.products == null)
this.products = new ArrayList<LCompProd>();
LCompProd compprod = new LCompProd(this, prod);
this.products.add(compprod);
compprod.setComponent(this);
}
@Entity
@Table(name = "sa_s_prod")
public class SProd implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int prodId;
@NotNull
private Product.Type prodType;
@NotNull
private String prodName;
private String prodDesc;
private BigDecimal prodQuantityDef;
private Globals.Units prodUnitDef;
private Lkzs lkz;
@Temporal(TemporalType.TIMESTAMP)
private Date cdate;
private String cuser;
@OneToOne(optional = false)
@JoinColumn(name = "subcatId", nullable = false)
private SSubCat subcat;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "transId", unique = true)
private STrans translation;
@OneToMany(mappedBy = "product", fetch = FetchType.EAGER)
private List<LCompProd> components;
@Entity
@IdClass(LCompProdId.class)
@Table(name = "sa_l_compprod")
public class LCompProd implements Serializable {
@Id
private int compId;
@Id
private int prodId;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "compId", insertable = false, updatable = false)
private SComp component;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "prodId", insertable = false, updatable = false)
private SProd product;
@NotNull
private Lkzs lkz;
@Temporal(TemporalType.TIMESTAMP)
private Date cdate;
private String cuser;
public class LCompProdId implements Serializable {
private int compId;
private int prodId;
....
使用此代码,我可以毫无问题地检索组件和相关产品。但是我不能坚持到联接表(CompProd)。即使没有与两个独立的人持续不断地进行这样做。
问题是外键约束失败,因为在持久化期间未在“连接数据对象”中设置组件的主键(compId)。
也许这是设置主/外键的“正常”行为,但我不能相信hibernate无法处理这个简单的事情?! (希望我是对的; - )
我阅读了很多文章和博客,尝试了不同的方法,但仍然无法使其发挥作用(以某种方式应该实施)。
任何帮助表示赞赏。 THX。
PS:我正在使用JPA2(Hibernate)。
答案 0 :(得分:0)
您似乎在 SComp 和 SProd 对象创建中获得 compId 和 prodId 值。
如何将生成的id传播到 LCompProd 实体?您确定从中获取值的对象是托管实体吗?
例如,如果使用merge调用创建对象,则该参数不会成为托管实体。因此,您不应直接获得生成的Id。你可以通过方法调用的结果得到它。