必须有两个表 Customer 和 Upload,我在其中跟踪客户上传的项目数量。它与共享主键是一对一的关系,共享主键CustomerID
是唯一标识符。我正在努力让保存工作。我正在使用 Hibernates uuid2 策略来处理唯一标识符的创建/保存。在我包含上传实体之前,客户将使用 uuid 正确保存。基本上,我希望在有人制作客户对象时创建上传。
顾客:
@Entity
public class Upload implements Serializable {
@Column(name="UPLOADCOUNT", nullable=true, unique=false)
private Integer uploadCount = new Integer(0);
@MapsId ("CUSTOMERID")
@OneToOne (optional=false)
@JoinColumn(name="CUSTOMERID", referencedColumnName = "CUSTOMERID", nullable=false, unique=true )
private Customer customer;
@Id
@Column(name="CUSTOMERID", length=36, nullable=false, unique=true, insertable=false, updatable=false)
private String customerId_;
public Upload(Customer customer) {
this.customer = customer;
}
}
上传:
@Entity
public class Customer implements Serializable {
@Id
@Column(name = "CUSTOMERID", unique = true)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String customerId;
@OneToOne(mappedBy = "customer", optional = false, cascade = CascadeType.ALL)
private Upload upload ;
public Customer() {
this.upload = new Upload(this);
}
}
要保存的代码:
Customer customer = new Customer();
EntityManager em = EntityManagerUtil.getEntityManager();
EntityTransaction trans = em.getTransaction();
trans.begin();
em.persist(customer);
trans.commit();
例外:
javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): Upload