Eclipse'从表'和'多对一'关系生成实体

时间:2016-11-05 16:18:29

标签: java mysql eclipse java-ee

我正在开发一个使用数据库的简单Java EE应用程序。它有两个表(Admin和Session签名)连接多对一关系。

当我使用eclipse从表生成实体时,我的属性links这两个表都是这样生成的:

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

问题是,我的owner属性在数据库中是Integer,并且已经创建为Admin类型。

现在,当我想传递一些Integer变量将其输入到数据库时,我得到错误:

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

或者,当我想将它(如此)转换为(管理员)时(从会话中获取):

(Admin)session.getAttribute("adminId")

我得到Jboss错误:

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)

我确信这可以轻松完成,但我使用Java时非常糟糕。

感谢您的帮助。

编辑:

我的Admin.java类:

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;
    }

}

1 个答案:

答案 0 :(得分:1)

你应该传递一个肯定有一个int id字段的Admin对象。

所以你要做这样的事情

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

修改

如果要将SignatureSession对象与Admin关联,则上述有效。相反,如果你在Session中有一个Admin ojbect,你只需要执行

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

或            Admin myAdmin = new Admin();            myAdmin.setId(ANID);

但是,我再说一遍,这取决于你在Session中拥有的内容以及你处理的对象。 而且,当您希望使用JPA时,不要忘记在对象上执行em.persist或em.merge之类的操作。 也许你应该更深入地了解JPA的工作原理。