java - Updating entity through JPA -


i doing project self-education , have stuck problem when trying update entity bound other entity. using spring boot 1.2.3.release + jpa.

person:

@component @entity @table(name = "person_sql", uniqueconstraints =         @uniqueconstraint(columnnames = {"name", "passport"}))  public class person implements academyentity {  @id @generatedvalue private long id;  private string name; private date birthday; @column(unique=true) private string passport;  @onetomany(cascade = cascadetype.all) @joincolumn(name = "person_id") private set<teacher> teacherset;  public person() {     this.birthday = new date();     studentset = new hashset<student>();     teacherset = new hashset<teacher>(); }  public person(string name, date birthday, string passport) {     this.name = name;     this.birthday = birthday;     this.passport = passport;     studentset = new hashset<student>();     teacherset = new hashset<teacher>(); }  getters, setters, equals, hashcode, tostring } 

teacher

@component @entity @table(name = "teacher_sql")  public class teacher implements academyentity {  @id @generatedvalue private long id; private date start; private date finish;  @manytoone @joincolumn(nullable = false) private person person;  ... constructors, getters, setters, equals, hashcode, tostring } 

jpapersondao

public interface jpapersondao extends jparepository<person, long> { } 

jpapersonservice

@service public class jpapersonservice implements personservice { private jpapersondao jpapersondao; ... @override public boolean saveservice(object object) {     if (object instanceof person) {         try {             if (validateperson((person) object)) {                 jpapersondao.save((person) object);                 return true;             }         } catch (runtimeexception e) {             log.error(e);         }     }     return false; } ... 

when i'm updating person not teacher - fine. if touch person teacher - error:

person person = new person(); person.setid(..); jpapersonservice.saveservice(person); ... warn 6308 --- [nio-8181-exec-6] o.h.engine.jdbc.spi.sqlexceptionhelper   : sql error: 23502, sqlstate: 23502 error 6308 --- [nio-8181-exec-6] o.h.engine.jdbc.spi.sqlexceptionhelper   : null not allowed column "person_id"; sql statement null not allowed column "person_id"; sql statement: update teacher_sql set person_id=null person_id=? [23502-185] info 6308 --- [nio-8181-exec-6] o.h.e.j.b.internal.abstractbatchimpl     : hhh000010: on release of batch still contained jdbc statements 

i assume might use transactions (@transactional) here, don't understand 'how-to'.
help!

edit
if use pure hibernate (without spring , jpa) same entities works fine.

public class hibernatepersondaoimpl implements persondao { ... private boolean saveordelete(person p, boolean delete) {     session session = hibernateutils.getsession();     try {         transaction t = session.begintransaction();         if (delete) {             session.delete(p);         } else {             session.merge(p);         }         t.commit();     } catch (hibernateexception e) {         e.printstacktrace();         return false;     }     return true; } 

i believe missing mappedby parameter on 1 of relations.

@onetomany(cascade = cascadetype.all, mappedby="person") private set<teacher> teacherset; 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -