2017-05-04 1 views
1

Hibernateを初めて使用しています。私は2つのテーブルTeam(親)とProduct(子供)TEAM_IDの列を持つ関係として、各チームは複数の製品を持ち、各製品は単一のチームを持っています。私はチームクラスで@OneToManyマッピングと@ManyToOneProductクラスのエンティティクラスを作成しました。1対多のマッピングアノテーションの問題を解決する

私がしようとしていたときのチームはすでに

利用可能な場合、チームは唯一の製品保存

  • 新しいとき、私は、製品とチームの両方を保存するにはシナリオ、

    1. の下に隠蔽する必要が

      製品を保存してチームを再度保存しようとすると、再び制約エラーがスローされます。 助けてください。

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

    2つ目のシナリオでは、データalreなどの簡単なアップデートでありますadyが存在するので、新しい挿入ではなく更新を行います。 – akuma8

    答えて

    0

    チームの商品リストを設定する唯一の時間は、チームが新しいエンティティの場合です。だから、私はそれを通過して、私を聞かせてください多くの関係にあなたに1つのためのいくつかのサンプルコードを与えるKNいくつかの問題があれば....私は私の条件を2.sku 1.製品2つのテーブルを持っている

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

    Maciejありがとうございます。私はあなたに試して更新します – user3800354

    0

    は、1であります製品には、SKUのの多くを持っている...

    Product.java

    @LazyCollection(LazyCollectionOption.FALSE) 
        @ElementCollection(targetClass=Product.class) 
        @OneToMany(mappedBy="product" , cascade=CascadeType.MERGE) 
        private List<Sku> listSkuOrders = new ArrayList<>(); 
    

    Sku.java

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = PRODUCT_ID , nullable = false) 
    private Product product; 
    
    +0

    ありがとうY2k。答えごとに、ALLからテーブルをマージして@LazyCollectionアノテーションを使用するだけで、カスケードタイプが変更されます。 @LazyCollectionの使用を知ってもいいですか? – user3800354

    +0

    gr8 ..私の答えを投票してください。http://stackoverflow.com/questions/12928402/what-is-the-use-of-the-hibernate-lazycollection-annotation – y2k

    関連する問題