1

I have two classes as following,

Human.java

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Human implements Serializable {

   private long id;
   private String name;
    ....
}

Student.java

@Entity
@DynamicUpdate
public class Student extends MyFactories {

    private List<Know> KnowList;

    @OneToMany(fetch = FetchType.LAZY)
    public List<Know> getKnowlist() {
        return knowlist;
    }

    public void setKnowlist(List<Know> KnowList) {
        return Knowlist;
    }
}

Know.java

@Entity
public class Know implements Serializable {

    private long id;
    private Human hu;
    private Student st;

    ....

    @ManyToOne 
    public Person getHu() {
        return hu;
    }

    @ManyToOne
    public Client getSt() {
        return st;
    }

    ....  setters .....
}

Code

            Know kw = new Know();
            kw.setSt(studentObject);
            kw.setHu(humanObject);
            session.save(kw);
            tx.commit();

I am able to insert into Know table but hibernate does not insert any record to student_know table which it has created.

I have found this answer but it says I need to use that method if I always want to retrieve all the records. Which I do not (at times, I may just need to retrieve the student class not list of its know)

    System.out.println(this.student.getKnowList().size());

When I try to access the list it runs into following exception.

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myproject.Student.knowList, could not initialize proxy - no Session
Community
  • 1
  • 1
Tim Norman
  • 421
  • 1
  • 12
  • 31
  • have you looked at this ?http://stackoverflow.com/questions/13199045/hibernate-many-to-one-mapping-should-i-insert-this-way – grepit Jul 30 '13 at 02:08
  • @CPU100 thanks but I have the factory and myfactories objects but not sure how to connect product table to myfactories and factory objects – Tim Norman Jul 30 '13 at 02:20
  • Student extends MyFactories? the getter `Client getSt()` and the variable `Student st` can your example code realy work? – Angga Jul 30 '13 at 04:28

2 Answers2

2

for select case change that @OneToMany(fetch = FetchType.LAZY) to @OneToMany(fetch = FetchType.EAGER) so you can get data inside it's list.

and for the insert i need your clarification about where is your relation or getter setter of the private Factory fac;?

you should have at least something like this :

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "YOUR_FACTORY_ID_COLUMN")
private Factory fac;

public Factory getFac(){
    return fac;
}

public void setFac(Factory fac){
    this.fac=fac;
}

and did factory have any id?

Angga
  • 2,305
  • 1
  • 17
  • 21
  • factory is inheriting its id from myfactories, I have included all the getter and setters, I do not want it to load all the products every time it is loading factory object as they are a lot. – Tim Norman Jul 30 '13 at 03:55
  • they will only loading all product inside that factory, your exception is because you didn't get the object(product) when you in a state that can't get the session. so the only way is to load it before you going to move this object to session-less state(maybe you send this Factory to client and try to get its Product). – Angga Jul 30 '13 at 04:03
  • it runs into this error EVERE: org.hibernate.MappingException: Repeated column in mapping for entity: com.myproject.Know column: id (should be mapped with insert="false" update="false") – Tim Norman Jul 30 '13 at 04:14
  • that's already a different problem, [see this article](http://www.javaworld.com/javaworld/jw-08-2008/jw-08-hibernate-annotations.html) for how to create **simple persistent class**. or post another question if you have difficulty creating those standard persistent class. – Angga Jul 30 '13 at 04:24
  • but the first problem still exists – Tim Norman Jul 30 '13 at 04:24
  • 1
    i edit the answer(add cascade), now try that, and if that didnt work post your edited code here(don't forget to read link i mentioned before to create id auto generated). – Angga Jul 30 '13 at 04:42
  • thanks but J888 posted the answer, the problem was that I was not updating the student table. thanks anyway – Tim Norman Jul 30 '13 at 04:53
  • 1
    you are welcome, however, you can still use cascade though if you want to save/edit/delete list without having another update. – Angga Jul 30 '13 at 04:58
1

You need to use session.Update(studentObject) as well, to insert a row into student_know table.

Please also be aware that access to a lazy association outside of the context of an open Hibernate session will result in an exception. Link

J888
  • 1,944
  • 8
  • 42
  • 76