Customer と Upload の 2 つのテーブルが必要です。ここで、顧客がアップロードしたアイテムの数を追跡します。CustomerID
これは、一意の識別子である共有主キーとの 1 対 1 の関係です。セーブを機能させるのに苦労しています。一意の識別子の作成/保存を処理するために、Hibernates uuid2 戦略を使用しています。Upload エンティティを含める前は、顧客は 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