【问题标题】:Hibernate Annotations Perform Inner JoinHibernate Annotations 执行内部连接
【发布时间】:2017-09-09 08:24:13
【问题描述】:

你好我刚开始学习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 包 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);


        }
        }

请帮我解决这个错误/在此先感谢。

【问题讨论】:

    标签: java hibernate hibernate-annotations


    【解决方案1】:

    Hibernate无法处理公共setter和私有字段的注解混合在一个类中。

    一个可能的解决方案是在公共 setter 上进行所有注释,而不是将其混合在私有字段和公共 setter 之间,这样可以避免public 和@987654323 都有注释的情况@ 访问修饰符。

    【讨论】:

    • 一般混合字段/getter JPA注解被禁止/不能工作。
    【解决方案2】:

    您的注释有冲突。这个:

    @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) 上的第一个注释。

    【讨论】:

    • 如果我删除这个@Column @ElementCollection(targetClass = AssemblyProperties.class)
    • 是的,删除上面的第一个注释。
    • 如果我删除这个@Column @ElementCollection(targetClass = AssemblyProperties.class) 我得到以下错误无法创建sessionFactory object.org.hibernate.MappingException:无法确定类型:java.util。设置,在表:程序集,列:[org.hibernate.mapping.Column(assembly_properties)]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多