gpt4 book ai didi

java - Hibernate一对多映射注释问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:08 25 4
gpt4 key购买 nike

我是 Hibernate 新手。我有两个表 Team(父)和 Product(子),其中 TEAM_ID 列作为关系,每个团队将有多个产品,每个产品将有一个团队。我在 Team 类中创建了带有 @OneToMany 映射的实体类,在 Product 类中创建了带有 @ManyToOne 映射的实体类。

我需要掩盖以下场景,

  1. 在新团队成立时保存产品和团队
  2. 如果团队已经可用,则仅保存产品

当我尝试保存产品时,它尝试再次保存团队抛出约束错误。请帮忙。

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);
}

最佳答案

只有当团队是一个新实体时,您才应该在团队上设置产品列表。所以:

Set<Product> products = new HashSet<Product>();
products.add(product);
if(getTeam(team.getTeamName()) != null) {
product.setTeam(getTeam(team.getTeamName()));
sessionFactory.getCurrentSession().saveOrUpdate(product);
} else {
team.setProducts(products);
sessionFactory.getCurrentSession().saveOrUpdate(team);
}

关于java - Hibernate一对多映射注释问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43783292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com