java - Eclipse 'generate entities from tables' and 'many to one' relation -
i developing simple java ee application, uses database. has 2 tables (admin , session signature) connected many 1 relation.
when used eclipse generate entities tables, attribute links
both tables generated this:
//bi-directional many-to-one association admin @manytoone @joincolumn(name="owner") private admin admin;
problem is, owner
attribute integer in database, , has been created admin type.
now when want pass integer variable input database error:
the method setadmin(admin) in type signaturesession not applicable arguments (int).
or when want cast (admin) (taking session):
(admin)session.getattribute("adminid")
i jboss error:
javax.servlet.servletexception: java.lang.classcastexception: java.lang.integer cannot cast com.podpisy.entities.admin javax.faces.webapp.facesservlet.service(facesservlet.java:606) secure.securitycheckfilter.dofilter(securitycheckfilter.java:100)
i sure can done easy, i'm bad using java.
thanks help.
edit:
my admin.java class:
package com.podpisy.entities; import java.io.serializable; import javax.persistence.*; import java.util.list; @entity @table(name="admins") @namedquery(name="admin.findall", query="select admin a") public class admin implements serializable { private static final long serialversionuid = 1l; @id private int id; private string login; private string password; //bi-directional many-to-one association signature @onetomany(mappedby="admin") private list<signature> signatures; //bi-directional many-to-one association signaturesession @onetomany(mappedby="admin") private list<signaturesession> signaturesessions; public admin() { } public int getid() { return this.id; } public void setid(int id) { this.id = id; } public string getlogin() { return this.login; } public void setlogin(string login) { this.login = login; } public string getpassword() { return this.password; } public void setpassword(string password) { this.password = password; } public list<signature> getsignatures() { return this.signatures; } public void setsignatures(list<signature> signatures) { this.signatures = signatures; } public signature addsignature(signature signature) { getsignatures().add(signature); signature.setadmin(this); return signature; } public signature removesignature(signature signature) { getsignatures().remove(signature); signature.setadmin(null); return signature; } public list<signaturesession> getsignaturesessions() { return this.signaturesessions; } public void setsignaturesessions(list<signaturesession> signaturesessions) { this.signaturesessions = signaturesessions; } public signaturesession addsignaturesession(signaturesession signaturesession) { getsignaturesessions().add(signaturesession); signaturesession.setadmin(this); return signaturesession; } public signaturesession removesignaturesession(signaturesession signaturesession) { getsignaturesessions().remove(signaturesession); signaturesession.setadmin(null); return signaturesession; } }
my signaturesession.class:
package com.podpisy.entities; import java.io.serializable; import javax.persistence.*; /** * persistent class signaturesession database table. * */ @entity @namedquery(name="signaturesession.findall", query="select s signaturesession s") public class signaturesession implements serializable { private static final long serialversionuid = 1l; @id private int id; private string device; private string name; private int signatures; private int time; private string type; private int users; //bi-directional many-to-one association admin @manytoone @joincolumn(name="owner") private admin admin; public signaturesession() { } public int getid() { return this.id; } public void setid(int id) { this.id = id; } public string getdevice() { return this.device; } public void setdevice(string device) { this.device = device; } public string getname() { return this.name; } public void setname(string name) { this.name = name; } public int getsignatures() { return this.signatures; } public void setsignatures(int signatures) { this.signatures = signatures; } public int gettime() { return this.time; } public void settime(int time) { this.time = time; } public string gettype() { return this.type; } public void settype(string type) { this.type = type; } public int getusers() { return this.users; } public void setusers(int users) { this.users = users; } public admin getadmin() { return this.admin; } public void setadmin(admin admin) { this.admin = admin; } }
you should pass admin
object surely has int id field.
so you've make this
admin myadmin=new admin(id,.. other properties); mysignaturesession.setadmin(myadmin);
edit
above valid if want associate , admin signaturesession object. instead if have admin ojbect in session have execute
admin anadmin=(admin)session.getattibute("adminid"); admin myadmin=new admin(id,.. other properties);
or admin myadmin=new admin(); myadmin.setid(anid);
but, repeat, depends have in session , objects handle. and, using jpa, dont forget em.persist or em.merge on objects. maybe should little deeper on how jpa works.
Comments
Post a Comment