Java Hibernate OneToOne Exception Mapping Error? -
i have follow classes
@entity @table(name = "seek") public class seek implements serializable { @id @generatedvalue(strategy = generationtype.identity) private int id; @notnull @valid @onetoone(mappedby="url") private url url; } @entity @table(name = "url") public class url implements serializable { @id @generatedvalue(strategy = generationtype.identity) private int id; @onetoone(cascade = cascadetype.all) @joincolumn(name = "idseek", referencedcolumnname = "id") private seek seek; }
my url table has idseek column, seek table doesn't have relationship column url table.
the exception receive is:
org.springframework.beans.factory.beancreationexception: error creating bean name 'seekdao': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private org.hibernate.sessionfactory dao.hibernate.abstractdao.sessionfactory; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'sessionfactory' defined in class path resource [configuration/hibernateconfig.class]: invocation of init method failed; nested exception org.hibernate.annotationexception: referenced property not (one|many)toone: model.url.url in mappedby of model.seek.url
my daos extend
public abstract class abstractdao<pk extends serializable, t> { private final class<t> persistentclass; public abstractdao() { this.persistentclass = (class<t>) ((parameterizedtype) this.getclass().getgenericsuperclass()).getactualtypearguments()[1]; } @autowired private sessionfactory sessionfactory; protected session getsession() { return this.sessionfactory.getcurrentsession(); } public t getbykey(pk key) { return (t) this.getsession().get(this.persistentclass, key); } public t getbycolumn(string column, string value) { criteria criteria = this.getsession().createcriteria(this.persistentclass); return (t) criteria.add(restrictions.eq(column, value)).uniqueresult(); } public void persist(t entity) { this.getsession().persist(entity); } public void delete(t entity) { this.getsession().delete(entity); } protected criteria createentitycriteria() { return this.getsession().createcriteria(this.persistentclass); } protected query getquery(string query) { return this.getsession().createquery(query); }
}
like that
@repository("seekdao") public class seekdaoimpl extends abstractdao<integer, seek> implements seekdao { @override public seek get(int id) { return super.getbykey(id); } @override public void save(seek seek) { super.persist(seek); } }
i don't know hibernate, problem?
edited
i did it, , compiles receive this: warn: sql error: 1048, sqlstate: 23000 error: column 'idseek' cannot null
my method @ service class is:
// service @transactional @override public void saveseek(seek seek) { this.seekdao.save(seek); this.urldao.save(seek.geturl());//the url inside seek obj }
so need insert id manually , put @ url obj? thanks
your seek class not pointing correct mappedby
property.
@entity @table(name = "seek") public class seek implements serializable { @id @generatedvalue(strategy = generationtype.identity) private int id; @notnull @valid @onetoone(mappedby="seek") private url url; }
Comments
Post a Comment