我将新对象保存到数据库时遇到问题。我正在使用Eclipselink和Postgresql。当我尝试添加新商品时,级联还添加了Price对象。
Price.java
@Entity
@Table(name = "Prices")
public class Price implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(updatable = false)
private int id;
@Column(updatable = false, nullable = false)
private float value;
@Column(updatable = false, nullable = false)
private Date startDate;
@Column(updatable = false, nullable = true)
private Date endDate;
@ManyToOne(targetEntity=Merchandise.class,cascade=CascadeType.ALL)
@JoinColumn(name="id",nullable = false,updatable = false,insertable=false)
private Merchandise merchandiseId;
@Column(updatable = false, nullable = true)
private float valueBulk;
@Column(updatable = false, nullable = true)
private float valueRetail;
@Column(updatable = false, nullable = true)
private float makeupPercent;
@Column(updatable = false, nullable = true)
private float makeupForce;
Merchandise.java
@Entity
@Table(name="Merchandises")
public class Merchandise implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(updatable = false)
private int id;
@Column(updatable = false)
private int externalId;
@Column(unique = false, nullable = false, updatable = true)
private String name;
@Column(unique = false, nullable = true, updatable = true)
private int available;
@Column(unique = false, nullable = true, updatable = true)
private String description;
@OneToMany(targetEntity = Price.class, mappedBy = "merchandiseId",cascade = CascadeType.PERSIST)
private List<Price> prices;
@OneToMany(targetEntity = MerchandiseCategory.class, mappedBy = "merchandiseId",cascade = CascadeType.PERSIST)
private List<MerchandiseCategory> merchandiseCategories;
@OneToMany(targetEntity = MerchandiseOrder.class, mappedBy = "merchandiseId",cascade=CascadeType.PERSIST)
private List<MerchandiseOrder> merchandiseOrders;
@OneToMany(targetEntity = MerchandiseDiscount.class, mappedBy = "merchandiseId",cascade=CascadeType.PERSIST) //#61
private List<MerchandiseDiscount> discounts; //#61
我在facede中的代码如下所示:
Dao dao = new DAO();//begins transaction etc.
Merchandise m = new Merchandise;
m.set..//setting all needed fields
List<Price> list = new ArrayList<Price>();
Price p = new Price();
p.set..//setting all needed fields
p.setMerchandiseID(m);
list.add(p);
m.setPrices(list);
dao.addMerchandise(m);//persisting
这会得到平均错误(在波兰语中我会试着翻译它):外键限制违规 - 商品中没有Key(id)=(356)。
我认为这是生成id的问题,id fiels总是被生成的id替换,我认为它们是不同的,但在这种情况下应该是相同的
答案 0 :(得分:0)
您的Price类使用其“id”字段作为Merchandise的外键。您还标记了要生成的ID属性映射,因此它将始终与Merchandise中的不同。您是否打算将Price用于商品的外键作为主键?如果是这样,您需要从Price中删除@GeneratedValue 1)需要先维护商品,刷新以便分配其主键,然后将值设置为p.setId()以及调用p.setMerchandiseID(m)。
或 2)使用JPA 2.0的派生ID功能,并将Price的merchandiseId属性标记为@Id(并删除不需要的int id),或使用@MapsId。 @Mapsid将允许JPA在为商品分配值时自动设置商品的ID值。 一些信息:http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/mappedbyid
如果您想让Price拥有自己独立的主键,则需要在价格表中添加外键:
@ManyToOne(targetEntity = Merchandise.class,级联= CascadeType.ALL) @JoinColumn(name =“fk_merchandiseid”,nullable = false) 私人商品商品;