2017-09-09 10 views
0

こんにちは私はちょうど休止状態を学習し始めました。私が間違っているところで私を修正してください。私は、ハイバネートアノテーションを使用している結合テーブルを使用して、2つのテーブルの間に1対多の関係をしたい。休止状態の注釈は内部結合を実行します

create table assembly 
(
    assembly_id serial primary key, 
    number  text, 
    user_id  int 
); 

    create table assembly_properties 
(
    property_id serial primary key, 
    property_name text, 
    property_type  text 
); 

create table assembly_properties_mapping 
(
mapping_id  serial primary key, 
assembly_id  int, 
property_id  int, 
property_value text, 
CONSTRAINT FK_assembly_id FOREIGN KEY (assembly_id) REFERENCES assembly(assembly_id), 
CONSTRAINT FK_property_id  FOREIGN KEY (property_id)  REFERENCES assembly_properties(property_id) 
    ); 

私はこれらの3つのテーブルをpostgres sqlデータベースに作成しました。以下は私のAssembly.class以下

 package com.development.wrapper; 


     @Entity 
     @Table(name = "assembly") 
     public class Assembly { 


      @Id 
      @GeneratedValue(strategy = GenerationType.IDENTITY) 
      @Column(name = "assembly_id") 
       private int assembly_id; 


      @Column(name = "number") 
      private String number; 



      @Column(name ="UserID") 
      private int userId; 


      @Column 
      @ElementCollection(targetClass = AssemblyProperties.class) 
      private Set<AssemblyProperties> assembly_properties; 

      public int getAssembly_id() { 
       return assembly_id; 
      } 

      public void setAssembly_id(int assembly_id) { 
       this.assembly_id = assembly_id; 
      } 

      public String getNumber() { 
       return number; 
      } 

      public void setNumber(String number) { 
       this.number = number; 
      } 

      public int getUserId() { 
       return userId; 
      } 

      public void setUserId(int userId) { 
       this.userId = userId; 
      } 

      @OneToMany(targetEntity = AssemblyProperties.class, cascade = CascadeType.ALL) 
      @JoinTable(name = "assembly_properties_mapping", joinColumns = { @JoinColumn(name = "assembly_id") }, inverseJoinColumns = { @JoinColumn(name = "property_id") }) 


      public Set<AssemblyProperties> getAssembly_properties() { 
       return assembly_properties; 
      } 

      public void setAssembly_properties(Set<AssemblyProperties> assembly_properties) { 
       this.assembly_properties = assembly_properties; 
      } 

     } 

はAssemblyProperties.class パッケージcom.development.wrapperされています。

 @Entity 
     @Table(name = "assembly_properties") 
     public class AssemblyProperties { 
      @Id 
      @GeneratedValue(strategy = GenerationType.IDENTITY) 
      @Column(name = "property_id") 
       private int property_id; 

      @Column(name = "property_name") 
      private String property_name; 

      @Column(name = "property_type") 
      private String property_type; 

      public int getProperty_id() { 
       return property_id; 
      } 

      public void setProperty_id(int property_id) { 
       this.property_id = property_id; 
      } 

      public String getProperty_name() { 
       return property_name; 
      } 

      public void setProperty_name(String property_name) { 
       this.property_name = property_name; 
      } 

      public String getProperty_type() { 
       return property_type; 
      } 

      public void setProperty_type(String property_type) { 
       this.property_type = property_type; 
      } 

     } 

私はエラーを取得しています下記のようにデータベーステーブル内のデータをロードしようとしているのSessionFactory object.org.hibernate.MappingExceptionの作成に失敗しました:com.development.wrapper:用のタイプを決定できませんでした。 AssemblyProperties、テーブルで:列に対してAssembly_assembly_properties、:[org.hibernate.mapping.Column(assembly_properties)] java.lang.ExceptionInInitializerError以下

は私が

を実行しようとしていたコードであるスレッドで 例外 "メイン"
 public class Test 
     { 
      SessionFactory factory; 

      public Test() throws Exception 
      { 

        try 
        { 
          factory = new AnnotationConfiguration().configure(). 
           addPackage("com.development.wrapper"). //add package if used. 
              addAnnotatedClass(Assembly.class).buildSessionFactory(); 
        } 
        catch (Throwable ex) 
        { 
          System.err.println("Failed to create sessionFactory object." + ex); 
          throw new ExceptionInInitializerError(ex); 
        } 

      } 


      public Integer addClass(Assembly assembly) 
      { 
        Session session = factory.openSession(); 
        Transaction tx = null; 
        Integer assemblyid = null; 

        try 
        { 
          tx = session.beginTransaction(); 

          assemblyid = (Integer) session.save(assembly); 
          System.out.println(assemblyid); 
          tx.commit(); 
        } 
        catch (HibernateException e) 
        { 
          if (tx != null) 
            tx.rollback(); 
          e.printStackTrace(); 
        } 
        finally 
        { 
          session.close(); 
        } 
        return assemblyid; 
      } 

     public static void main(String[] args) throws Exception { 
      Set<AssemblyProperties> assemblyProperties = new HashSet<AssemblyProperties>(); 
      AssemblyProperties ass=new AssemblyProperties(); 
      ass.setProperty_name("xx"); 
      ass.setProperty_type("List"); 
      assemblyProperties.add(ass); 

      Assembly assembly =new Assembly(); 
      assembly.setAssembly_properties(assemblyProperties); 
      assembly.setNumber("aaa"); 
      assembly.setUserId(1); 
      Test test=new Test(); 
      test.addClass(assembly); 


     } 
     } 

このエラーを解決するにはどうすればよいですか/事前に感謝します。

答えて

0

Hibernateパブリックセッターとプライベートフィールドの注釈が1つのクラスで混在している場合は処理できません。

可能な解決策は、プライベートフィールドとパブリックセッターの間でパブリックセッターですべてのアノテーションを作成することです。つまり、publicprivateのアクセス修飾子の両方にアノテーションがある場合を避けることができます。

+0

一般にフィールド/ゲッタJPAアノテーションを混合することは禁止されています/動作できません。 –

0

あなたの注釈は競合しています。この:

@Column 
@ElementCollection(targetClass = AssemblyProperties.class) 
private Set<AssemblyProperties> assembly_properties; 

と、この:

@OneToMany(targetEntity = AssemblyProperties.class, cascade = CascadeType.ALL) 
@JoinTable(name = "assembly_properties_mapping", joinColumns = { @JoinColumn(name = "assembly_id") }, inverseJoinColumns = { @JoinColumn(name = "property_id") }) 
public Set<AssemblyProperties> getAssembly_properties() { 
       return assembly_properties; 
      } 

だけのプライベートフィールド上に第一annotaions(assembly_properties)を削除します。

+0

この@Columnを削除した場合 @ ElementCollection(targetClass = AssemblyProperties.class) – Saurabh

+0

はい、上記の最初のアノテーションを削除します。 – Moodi

+0

この@Columnを削除した場合@ElementCollection(targetClass = AssemblyProperties.class)以下のエラーが発生しましたsessionFactoryを作成できませんでしたobject.org.hibernate.MappingException:型を判別できませんでした:java.util.Set at table:assembly 、列の場合:[org.hibernate.mapping.Column(assembly_properties)] – Saurabh

関連する問題