Macjej Macjej - 1 month ago 7
Java Question

Eclipse 'generate entities from tables' and 'many to one' relation

i am developing a simple Java EE application, that uses database. It has two tables (Admin and Session signature) connected with many to one relation.

When i used eclipse to generate entities from tables, my attribute that

links
both tables was generated like this:

//bi-directional many-to-one association to Admin
@ManyToOne
@JoinColumn(name="owner")
private Admin admin;


Problem is, my
owner
attribute is Integer in database, and it has been created as Admin type.

Now when i want to pass some Integer variable to input it to database i get error:

The method setAdmin(Admin) in the type Signaturesession is not applicable for arguments (int).


Or when i want to cast it to (Admin) like this (taking it from session):

(Admin)session.getAttribute("adminId")


I get Jboss Error:

javax.servlet.ServletException: java.lang.ClassCastException: java.lang.Integer cannot be cast to com.podpisy.entities.Admin
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
secure.SecurityCheckFilter.doFilter(SecurityCheckFilter.java:100)


I am sure that this can be done easy, but i'm just really bad using Java.

Thanks for any 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 a FROM 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 to Signature
@OneToMany(mappedBy="admin")
private List<Signature> signatures;

//bi-directional many-to-one association to 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.*;


/**
* The persistent class for the signaturesession database table.
*
*/
@Entity
@NamedQuery(name="Signaturesession.findAll", query="SELECT s FROM 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 to 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;
}

}

Answer

You should pass an Admin object which surely has an int id field.

So you've to make something like this

  Admin myAdmin=new Admin(id,.. other properties);
  mySignaturesession.setAdmin(myAdmin); 

EDIT

Above is valid if you want to associate and Admin to your SignatureSession object. Instead if you have an Admin ojbect in Session you just have to execute

       Admin anAdmin=(Admin)session.getAttibute("adminId");
       Admin myAdmin=new Admin(id,.. other properties);

or Admin myAdmin=new Admin(); myAdmin.setId(anId);

But, i repeat, it depends from what you have in the Session and which objects you handle. And, as you look to be using JPA, dont forget to do something like em.persist or em.merge on your objects. Maybe you should get a little deeper on how JPA works.