Hibernate Annotations Perform Inner Join

1.2k views Asked by At

Hello I have just started learning hibernate. Please correct me where I am doing mistake. I want do a one-to-many relationship between two tables using a join table using hibernate annotations.

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

I have created these three table in postgres sql database. Below is my 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;
            }

        }

Below is 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;
            }

        }

When I am trying to load data in database table as given below I am getting an error Failed to create sessionFactory object.org.hibernate.MappingException: Could not determine type for: com.development.wrapper.AssemblyProperties, at table: Assembly_assembly_properties, for columns: [org.hibernate.mapping.Column(assembly_properties)] Exception in thread "main" java.lang.ExceptionInInitializerError

below is code I am trying to run

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


        }
        }

Please help me to resolve this error/ Thanks in advance.

2

There are 2 answers

1
Gherbi Hicham On

Hibernate can't handle when the annotations for public setters and private fields are mixed in one class.

A possible solution would be to make all of your annotations at the public setters instead of mixing it between the private fields and the public setters, that way you can avoid the case where there are annotations both at public and private access modifiers.

3
Moodi On

Your annotations are conflicting. this:

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

and this:

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

just remove first annotaions over private field (assembly_properties).