0

我正在使用 EJB、JPA 和 netbeans 开发一个 java web EE 应用程序。我创建了一个名为users的用于注册和登录的 sql 表和另一个名为prescripts的表,它有 3 个外键,指的是用户的主键 idusers(docid、pharmid、patid)。我还使用 net beans 创建了名为 users 的实体 bean 和一个名为 UsersFacade 的会话 bean,并为 prescripts 创建了一个实体 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 和 get 方法以及一些变量。请任何帮助都非常感谢,我现在被这两周困住了:'(


我向您发送所有类别的用户和规定:

处方.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 + "]";
    }

}

用户.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;
    }





}
4

2 回答 2

1

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

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

您应该阅读一两个 EJB3 教程——其中有很多,并确保您完成了这些教程。你似乎错过了一些基础知识。另请注意,您的代码可能要简单得多。通常不需要“@Column”注释,通常不需要 mapped_by,列名同上等。也使用单数名称(用户而不是用户)。主键的通用标准是简单@Id Long id的,可以很容易地记住所有实体的主键名称(但有些人更喜欢不同的名称)。

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

于 2012-05-20T20:12:07.840 回答
0

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

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

(“关系数据库”中的“关系”一词实际上只是表示表中的列(元组)相互关联……外键是一个附加项。)

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

于 2012-05-20T20:43:32.723 回答