java - object references an unsaved transient instance error -
ok want mapped groups , student in 1 table i've got error: "object references unsaved transient instance". if add cascade objects arleady exists. wrong ?
here entities:
@entity @table(name = "listaosob") public class listaosob implements serializable { @id @generatedvalue private int id; private static final long serialversionuid = 1l; @manytoone private student student; @manytoone private grupa grupa; @entity @table(name = "student") public class student implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue private int id; private string imie; private string nazwisko; private string login; private string haslo; @column(unique=true) private int nr_albumu; @column(unique=true) private string kod_karty; @entity @table(name = "grupa") public class grupa implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue private int id; private string nazwa; private time godzina; private string sala; private string tydzien; @manytoone private prowadzacy prowadzacy;
ejb code:
@stateless @localbean public class studentbean implements studentbeanremote, studentbeanlocal { @persistencecontext private entitymanager em; public studentbean() {} public void dodajstudenta(student s) { em.persist(s); em.flush(); } public void usunstudenta(student s) { em.remove(s); em.flush(); } @suppresswarnings("unchecked") public arraylist<student> listastudentow() { query query = em.createquery("select e student e"); return (arraylist<student>) query.getresultlist(); } @suppresswarnings("unchecked") public arraylist<student> listastudentow(grupa grupa) { query query = em.createquery("select e listaosob e e.grupa = :grupa"); query.setparameter("grupa", grupa); return (arraylist<student>) query.getresultlist(); } public void dodajgrupe(grupa g) { em.persist(g); } public void usungrupe(grupa g) { em.remove(g); em.flush(); } public void dodajlisteosob(listaosob l) { em.persist(l); }
and example test:
student name1 = new student("new"+gen.nextint(),"nazwisko","log", "haslo", "asd"+gen.nextint(), gen.nextint()); obj.dodajstudenta(name1); grupa n = new grupa(); obj.dodajgrupe(n); listaosob lista = new listaosob(name1, n); obj.dodajlisteosob(lista);
dodajlisteosob
throws object references unsaved transient instance
- because
listaosob
containsgrupa
(@manytoone
), not present in database grupa
not present in database, becausedodajgrupe
notpersist
other methods do- also
dodajlisteosob
not callpersist
(in scenario not cause issues)
Comments
Post a Comment