我是Hibernate的新手。我有兩個表Team
(父母)和Product
(子女)與TEAM_ID
列作爲關係,每個團隊將有多個產品,每個產品將有單個團隊。我已經創建了實體類,其中@OneToMany
在Team類中映射,@ManyToOne
在Product
類中映射。休眠一對多映射註解問題
我需要掩飾下面的場景,
- 爲了節省產品和團隊,當團隊是新
- 僅保存的產品,如果球隊已經可用
當我試圖它保存的產品嚐試再次保存團隊拋出約束錯誤。 請幫忙。
Team:
@Entity
@Table(name = "TEAM")
public class Team implements Serializable{
private static final long serialVersionUID = 5819170381583611288L;
@Id
@SequenceGenerator(name="teamIdSeq",sequenceName="team_id_seq",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="teamIdSeq")
@Column(name="TEAM_ID", updatable = false, nullable = false, unique = true)
private int teamId;
@Column(name="NAME", nullable = false, unique = true)
private String teamName;
@Column(name="DESCRIPTION", nullable = false)
private String teamDesc;
@Column(name="CONTACTS", nullable = false)
private String contacts;
@Column(name="APPROVER_NAME", nullable = false)
private String approverName;
@Column(name="APPROVAL_STATUS", nullable = false)
private int approvalStatus;
@Temporal(TemporalType.DATE)
@Column(name="CREATED_ON", nullable = false)
private Date createdOn;
@Column(name="CREATED_BY", nullable = false)
private String createdBy;
@Temporal(TemporalType.DATE)
@Column(name="MODIFIED_ON", nullable = false)
private Date modifiedOn;
@Column(name="MODIFIED_BY", nullable = false)
private String modifiedBy;
@OneToMany(fetch = FetchType.LAZY, mappedBy="team", cascade = CascadeType.ALL)
private Set<Product> products;
//setters and getters
}
Product:
@Entity
@Table(name = "PRODUCT", uniqueConstraints = {@UniqueConstraint(columnNames = {"PRODUCT_ID", "TEAM_ID"})})
public class Product implements Serializable{
private static final long serialVersionUID = 5819170381583611288L;
@Id
@SequenceGenerator(name="productIdSeq", sequenceName="product_id_seq",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="productIdSeq")
@Column(name="PRODUCT_ID", updatable = false, nullable = false)
private int productId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TEAM_ID")
private Team team;
@Column(name="NAME", nullable = false, unique = true)
private String productName;
@Column(name="DESCRIPTION", nullable = true)
private String productDesc;
@Column(name="APPROVER_NAME", nullable = false)
private String approverName;
@Column(name="APPROVAL_STATUS", nullable = false)
private int approvalStatus;
@Temporal(TemporalType.DATE)
@Column(name="CREATED_ON", nullable = false)
private Date createdOn;
@Column(name="CREATED_BY", nullable = false)
private String createdBy;
@Temporal(TemporalType.DATE)
@Column(name="MODIFIED_ON", nullable = false)
private Date modifiedOn;
@Column(name="MODIFIED_BY", nullable = false)
private String modifiedBy;
@OneToMany(fetch = FetchType.LAZY, mappedBy="product")
private Set<Form> forms;
//setters and getters
}
DAO:
@Repository
@EnableTransactionManagement
public class KMDBDAOImpl implements KMDBDAO {
@Autowired
private SessionFactory sessionFactory;
public void addTeam(Team team) {
Product product = new Product(team, "BMA" + Math.random(), "UI Tool", "test",
1, new Date(), "test", new Date(), "test");
Set<Product> products = new HashSet<Product>();
products.add(product);
team.setProducts(products);
if(getTeam(team.getTeamName()) != null) {
product.setTeam(getTeam(team.getTeamName()));
sessionFactory.getCurrentSession().saveOrUpdate(product);
} else {
sessionFactory.getCurrentSession().saveOrUpdate(team);
}
}
public Team getTeam(String teamName) {
Query query = sessionFactory.getCurrentSession().createQuery("from Team where teamName = :name");
query.setString("name", "teamName");
return (query.list().size() > 0 ? (Team) query.list().get(0) : null);
}
第二種情況是一個簡單的更新數據ALRE ady存在,所以進行更新而不是新插入 – akuma8