Hibernate注释执行内部联接

时间:2017-09-09 08:24:13

标签: java hibernate hibernate-annotations

您好我刚刚开始学习hibernate。请纠正我在哪里做错了。我想使用hibernate注释使用连接表在两个表之间进行一对多关系。

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

我在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             package 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)] 线程“main”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);


        }
        }

请帮我解决此错误/提前致谢。

2 个答案:

答案 0 :(得分:0)

当公共setter和私有字段的注释混合在一个类中时,

Hibernate无法处理。

一种可能的解决方案是在公共设置者处制作所有注释,而不是在私有字段和公共设置者之间混合,这样就可以避免在publicprivate处都有注释的情况。 my-project\complete\src\main\resources\templates http://localhost:8080/static/templates/forgotemail/images/bottompodmiddleb.jpg 访问修饰符。

答案 1 :(得分: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;
            }

只需删除私有字段(assembly_properties)上的第一个注释。