为什么ejb中的外键声明为对象(实体)?

时间:2012-05-14 12:00:26

标签: sql java-ee jpa netbeans ejb

我正在使用EJB,JPA和netbeans开发一个java web EE应用程序。我创建了一个名为 users 的用于注册和登录的表以及另一个名为 prescripts 的表,其中有3个外键引用用户的主键iduse(docid,pharmid ,patid)。 我还使用名为users的net beans实体bean和名为UsersFacade的会话bean创建了一个实体bean:prescripts和session bean:PrescriptsFacade。

我的问题是:

为什么在用户ejb中所有变量(列)都被声明为(字符串为字符串,整数为整数等)并且在处方中声明为用户?

//users.java
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idusers")
private Integer idusers;
@Column(name = "user_type")
private String userType;
@Column(name = "name")
private String name;
@Column(name = "nickname")
private String nickname;

//prescripts.java
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idprescripts")
private Integer idprescripts;
@Column(name = "data")
private String data;
@JoinColumn(name = "pharm_id", referencedColumnName = "idusers")
@ManyToOne
private Users users;
@JoinColumn(name = "doc_id", referencedColumnName = "idusers")
@ManyToOne
private Users users1;
@JoinColumn(name = "pat_id", referencedColumnName = "idusers")
@ManyToOne
private Users users2;

对于用户,我在servlet中使用此代码在sql base中插入一行:

Users currentUsers;
currentUsers = new Users();
String Type = request.getParameter("user_type");
String Name = request.getParameter("name");
String Nickname = request.getParameter("nickname");
currentUsers.setUserType(Type);
currentUsers.setName(Name);
currentUsers.setNickname(Nickname);
UsersFacade.create(currentUsers);

我该如何以这种方式在处方中插入一行?

这不起作用(它显示错误空指针异常):

currentPresc = new Prescripts();
String PatID = request.getParameter("pat_id");
String DocID = request.getParameter("doc_id");
String PharmID = request.getParameter("pharm_id");
String Data = request.getParameter("data");
int patid = Integer.parseInt(PatID);
int docid = Integer.parseInt(DocID);
int pharmid = Integer.parseInt(PharmID);
currentUsers = UsersFacade.find(patid);
currentPresc.setUsers(currentUsers);
currentUsers = UsersFacade.find(docid);
currentPresc.setUsers1(currentUsers);
currentUsers = UsersFacade.find(pharmid);
currentPresc.setUsers2(currentUsers);
currentPresc.setData(Data);
PrescriptsFacade.create(currentPresc);

为了简化原因,我跳过了set并获取了方法和一些变量。请真的非常感谢任何帮助,我现在被困2周了:'(


我发给你全部用户和处方类:

Prescripts.java

package entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;


@Entity
@Table(name = "prescripts")
@NamedQueries({
    @NamedQuery(name = "Prescripts.findAll", query = "SELECT p FROM Prescripts p"),
    @NamedQuery(name = "Prescripts.findByIdprescripts", query = "SELECT p FROM Prescripts p WHERE p.idprescripts = :idprescripts"),
    @NamedQuery(name = "Prescripts.findByData", query = "SELECT p FROM Prescripts p WHERE p.presc = :presc")})
public class Prescripts implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idprescripts")
    private Integer idprescripts;

    @JoinColumn(name = "doc_id", referencedColumnName = "idusers")
    @ManyToOne
    private Users doc_id;

    @JoinColumn(name = "pat_id", referencedColumnName = "idusers")
    @ManyToOne
    private Users pat_id;

    @JoinColumn(name = "pharm_id", referencedColumnName = "idusers")
    @ManyToOne
    private Users pharm_id;
    @Column(name = "presc")
    private String presc;

    public Prescripts() {
    }

    public Prescripts(Users pat_id, Users pharm_id, Users doc_id, String presc) {

        this.pharm_id = pharm_id;
        this.doc_id = doc_id;
        this.pat_id = pat_id;
        this.presc = presc;


    }

    public Integer getIdprescripts() {
        return idprescripts;
    }

    public void setIdprescripts(Integer idprescripts) {
        this.idprescripts = idprescripts;
    }

    public String getPresc() {
        return presc;
    }

    public void setPresc(String presc) {
        this.presc = presc;
    }


    public Users getPharmId() {
        return pharm_id;
    }

    public void setPharmId(Users pharm_id) {
        this.pharm_id = pharm_id;
    }


    public Users getDocId() {
        return doc_id;
    }

    public void setDocId(Users doc_id) {
        this.doc_id = doc_id;
    }

    public Users getPatId() {
        return pat_id;
    }

    public void setPatId(Users pat_id) {
        this.pat_id = pat_id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idprescripts != null ? idprescripts.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Prescripts)) {
            return false;
        }
        Prescripts other = (Prescripts) object;
        if ((this.idprescripts == null && other.idprescripts != null) || (this.idprescripts != null && !this.idprescripts.equals(other.idprescripts))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Prescripts[idprescripts=" + idprescripts + "]";
    }

}

Users.java

package entities;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlTransient;


@Entity
@Table(name = "users")
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByIdusers", query = "SELECT u FROM Users u WHERE u.idusers = :idusers"),
    @NamedQuery(name = "Users.findByUserType", query = "SELECT u FROM Users u WHERE u.userType = :userType"),
    @NamedQuery(name = "Users.findByNickname", query = "SELECT u FROM Users u WHERE u.nickname = :nickname"),
    @NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name = :name"),
    @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
    @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
    @NamedQuery(name = "Users.findByCity", query = "SELECT u FROM Users u WHERE u.city = :city"),
    @NamedQuery(name = "Users.findByStreet", query = "SELECT u FROM Users u WHERE u.street = :street"),
    @NamedQuery(name = "Users.findByAt", query = "SELECT u FROM Users u WHERE u.at = :at"),
    @NamedQuery(name = "Users.findByAmka", query = "SELECT u FROM Users u WHERE u.amka = :amka"),
    @NamedQuery(name = "Users.findByAfm", query = "SELECT u FROM Users u WHERE u.afm = :afm"),
    @NamedQuery(name = "Users.findByVerify", query = "SELECT u FROM Users u WHERE u.verify = :verify")})
public class Users implements Serializable {
    @OneToMany(mappedBy = "pat_id")
    private List<Prescripts> prescriptsList;
    @OneToMany(mappedBy = "doc_id")
    private List<Prescripts> prescriptsList1;
    @OneToMany(mappedBy = "pharm_id")
    private List<Prescripts> prescriptsList2;



    private static final long serialVersionUID = 1L;
    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idusers")
    private Integer idusers;

    @Column(name = "user_type")
    private String userType;
    @Column(name = "name")
    private String name;
    @Column(name = "nickname")
    private String nickname;
    @Column(name = "password")
    private String password;
    @Column(name = "email")
    private String email;
    @Column(name = "city")
    private String city;
    @Column(name = "street")
    private String street;
    @Column(name = "AT")
    private String at;
    @Column(name = "AMKA")
    private String amka;
    @Column(name = "AFM")
    private String afm;
    @Column(name = "verify")
    private Boolean verify;

    public Users() {
    }

        public Users( String userType,String name,String nickname, String password, String email, String city, String street, String at,
            String amka, String afm, Boolean verify)
    {   
        this.userType= userType;
        this.name= name;
        this.nickname= nickname;
        this.password= password;
        this.email = email;
        this.city = city;
        this.street = street;
        this.at = at;
        this.amka = amka;
        this.afm = afm;
        this.verify=verify;

    }

    public Integer getIdusers() {
        return idusers;
    }

    public void setIdusers(Integer idusers) {
        this.idusers = idusers;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getAt() {
        return at;
    }

    public void setAt(String at) {
        this.at = at;
    }

    public String getAmka() {
        return amka;
    }

    public void setAmka(String amka) {
        this.amka = amka;
    }

    public String getAfm() {
        return afm;
    }

    public void setAfm(String afm) {
        this.afm = afm;
    }

    public Boolean getVerify() {
        return verify;
    }

    public void setVerify(Boolean verify) {
        this.verify = verify;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idusers != null ? idusers.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.idusers == null && other.idusers != null) || (this.idusers != null && !this.idusers.equals(other.idusers))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Users[idusers=" + idusers + "]";
    }



    public List<Prescripts> getPrescriptsList() {
        return prescriptsList;
    }

    public void setPrescriptsList(List<Prescripts> prescriptsList) {
        this.prescriptsList = prescriptsList;
    }

    public List<Prescripts> getPrescriptsList1() {
        return prescriptsList1;
    }

    public void setPrescriptsList1(List<Prescripts> prescriptsList1) {
        this.prescriptsList1 = prescriptsList1;
    }

    public List<Prescripts> getPrescriptsList2() {
        return prescriptsList2;
    }

    public void setPrescriptsList2(List<Prescripts> prescriptsList2) {
        this.prescriptsList2 = prescriptsList2;
    }





}

2 个答案:

答案 0 :(得分:1)

主要问题似乎是为什么User-class中的变量被声明为Integer,但Prescript中的“user_id-variable”被声明为User。

这就是EJB3的工作方式。您正在考虑sql和关系数据库。您可以将表的主键(例如Users.idusers)定义为列,但使用自然对象定义对其他对象(确切地说是实体)的引用。因此,Prescripts.users被声明为Users-object。 EJB平台将负责将其转换为数据库列(在本例中可能命名为users_idusers),具有正确的类型(在本例中为Integer),但这需要处理,您不需要关心它。

你应该参加一两个EJB3教程 - 有很多这样的教程,并确保你完成教程。你好像错过了一些基础知识。另请注意,您的代码可能更简单。通常不需要“@Column”注释,通常不需要mapped_by,列名同上,等等。也使用单数名称(User而不是Users)。 primarykeys的通用标准只是@Id Long id,因此很容易记住所有实体的主键名称(但有些更喜欢不同的名称)。

要回答您的实际问题,我们需要更多信息,包括TestServlet.java第233行的内容。您的代码似乎或多或少是正确的,但其他人很难验证。从EntityManger / facade中查找Users-object,然后将其设置为Prescipts-object是正确的方法。

答案 1 :(得分:0)

在java中,实体之间的关系(在实体 - 关系模型中)表示为对象的引用,(一对一)或对象引用列表(一对多)

关系数据库实现实体(表中的行)之间的关系作为外键,与另一个表中的行的主键匹配。

(“关系数据库”中的“关系”一词实际上只是表示表中的列(元组)彼此相关...外键的东西是一个附加物。)

在设计数据库表之前,我通常认为从一个正确的实体关系模型开始是聪明的......