JPA ManytoOne双向

时间:2015-12-14 14:35:50

标签: java spring hibernate maven jpa

我使用web serice maven和jersey一起使用数据库sql server和jpa 和netbeans。 当我想登录时,我有一个问题.getPersonne(); print返回null; 我不明白为什么你能帮我。

谢谢你

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.ietevents.serviceweb.Entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
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;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author IETEVENTS
 */
@Entity
@Table(name = "Login")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Login.findAll", query = "SELECT l FROM Login l"),
    @NamedQuery(name = "Login.connexion", query = "SELECT l FROM Login l WHERE l.login = :login and l.password = :password"),
    @NamedQuery(name = "Login.findByKeyLogin", query = "SELECT l FROM Login l WHERE l.keyLogin = :keyLogin"),
    @NamedQuery(name = "Login.findByActif", query = "SELECT l FROM Login l WHERE l.actif = :actif"),
    @NamedQuery(name = "Login.findByDateCreation", query = "SELECT l FROM Login l WHERE l.dateCreation = :dateCreation"),
    @NamedQuery(name = "Login.findByKeyPersonne", query = "SELECT l FROM Login l WHERE l.personne = :personne"),
    @NamedQuery(name = "Login.findByLogin", query = "SELECT l FROM Login l WHERE l.login = :login"),
    @NamedQuery(name = "Login.findByPassword", query = "SELECT l FROM Login l WHERE l.password = :password")})
public class Login implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "KeyLogin")
    private Integer keyLogin;
    @Size(max = 255)
    @Column(name = "Actif")
    private String actif;
    @Column(name = "DateCreation")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCreation;
    @ManyToOne()
    @JoinColumn(name = "KeyPersonne",referencedColumnName = "KeyPersonne")
    private Personne personne;
    @Size(max = 255)
    @Column(name = "Login")
    private String login;
    @Size(max = 255)
    @Column(name = "Password")
    private String password;

    public Login() {
    }

    public Login(Integer keyLogin) {
        this.keyLogin = keyLogin;
    }

    public Integer getKeyLogin() {
        return keyLogin;
    }

    public void setKeyLogin(Integer keyLogin) {
        this.keyLogin = keyLogin;
    }

    public String getActif() {
        return actif;
    }

    public void setActif(String actif) {
        this.actif = actif;
    }

    public Date getDateCreation() {
        return dateCreation;
    }

    public void setDateCreation(Date dateCreation) {
        this.dateCreation = dateCreation;
    }

    public Personne getPersonne() {
        return personne;
    }

    public void setPersonne(Personne personne) {
        this.personne = personne;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (keyLogin != null ? keyLogin.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 Login)) {
            return false;
        }
        Login other = (Login) object;
        if ((this.keyLogin == null && other.keyLogin != null) || (this.keyLogin != null && !this.keyLogin.equals(other.keyLogin))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "fr.ietevents.serviceweb.Entity.Login[ keyLogin=" + keyLogin + " ]";
    }

}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.ietevents.serviceweb.Entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
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.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author IETEVENTS
 */
@Entity
@Table(name = "Personne")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Personne.findAll", query = "SELECT p FROM Personne p"),
    @NamedQuery(name = "Personne.findByKeyPersonne", query = "SELECT p FROM Personne p WHERE p.keyPersonne = :keyPersonne"),
    @NamedQuery(name = "Personne.findByActif", query = "SELECT p FROM Personne p WHERE p.actif = :actif"),
    @NamedQuery(name = "Personne.findByDateCreation", query = "SELECT p FROM Personne p WHERE p.dateCreation = :dateCreation"),
    @NamedQuery(name = "Personne.findByDateModification", query = "SELECT p FROM Personne p WHERE p.dateModification = :dateModification"),
    @NamedQuery(name = "Personne.findByEmail", query = "SELECT p FROM Personne p WHERE p.email = :email"),
    @NamedQuery(name = "Personne.findByFax", query = "SELECT p FROM Personne p WHERE p.fax = :fax"),
    @NamedQuery(name = "Personne.findByKeyCivilite", query = "SELECT p FROM Personne p WHERE p.keyCivilite = :keyCivilite"),
    @NamedQuery(name = "Personne.findByKeySociete", query = "SELECT p FROM Personne p WHERE p.keySociete = :keySociete"),
    @NamedQuery(name = "Personne.findByKeyTypeUser", query = "SELECT p FROM Personne p WHERE p.TypeUser = :TypeUser"),
    @NamedQuery(name = "Personne.findByNom", query = "SELECT p FROM Personne p WHERE p.nom = :nom"),
    @NamedQuery(name = "Personne.findByPortable", query = "SELECT p FROM Personne p WHERE p.portable = :portable"),
    @NamedQuery(name = "Personne.findByPrenom", query = "SELECT p FROM Personne p WHERE p.prenom = :prenom"),
    @NamedQuery(name = "Personne.findByTelephone", query = "SELECT p FROM Personne p WHERE p.telephone = :telephone")})
public class Personne implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "KeyPersonne")
    private Integer keyPersonne;
    @Size(max = 255)
    @Column(name = "Actif")
    private String actif;
    @Column(name = "DateCreation")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCreation;
    @Column(name = "DateModification")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateModification;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 255)
    @Column(name = "Email")
    private String email;
    // @Pattern(regexp="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$", message="Invalid phone/fax format, should be as xxx-xxx-xxxx")//if the field contains phone or fax number consider using this annotation to enforce field validation
    @Size(max = 255)
    @Column(name = "Fax")
    private String fax;
    @Column(name = "KeyCivilite")
    private Integer keyCivilite;
    @Column(name = "KeySociete")
    private Integer keySociete;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "KeyTypeUser")
    private TypeUser TypeUser;
    @Size(max = 255)
    @Column(name = "Nom")
    private String nom;
    @Size(max = 255)
    @Column(name = "Portable")
    private String portable;
    @Size(max = 255)
    @Column(name = "Prenom")
    private String prenom;
    @Size(max = 255)
    @Column(name = "Telephone")
    private String telephone;
    @OneToMany(cascade = CascadeType.ALL,mappedBy = "personne")
    private Collection<Login> ListLogin;

    public Personne() {
    }

    public Personne(Integer keyPersonne) {
        this.keyPersonne = keyPersonne;
    }

    public Collection<Login> getListLogin() {
        return ListLogin;
    }

    public void setListLogin(Collection<Login> ListLogin) {
        this.ListLogin = ListLogin;
    }

    public TypeUser getTypeUser() {
        return TypeUser;
    }

    public void setTypeUser(TypeUser TypeUser) {
        this.TypeUser = TypeUser;
    }

    public Integer getKeyPersonne() {
        return keyPersonne;
    }

    public void setKeyPersonne(Integer keyPersonne) {
        this.keyPersonne = keyPersonne;
    }

    public String getActif() {
        return actif;
    }

    public void setActif(String actif) {
        this.actif = actif;
    }

    public Date getDateCreation() {
        return dateCreation;
    }

    public void setDateCreation(Date dateCreation) {
        this.dateCreation = dateCreation;
    }

    public Date getDateModification() {
        return dateModification;
    }

    public void setDateModification(Date dateModification) {
        this.dateModification = dateModification;
    }

    public String getEmail() {
        return email;
    }

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

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public Integer getKeyCivilite() {
        return keyCivilite;
    }

    public void setKeyCivilite(Integer keyCivilite) {
        this.keyCivilite = keyCivilite;
    }

    public Integer getKeySociete() {
        return keySociete;
    }

    public void setKeySociete(Integer keySociete) {
        this.keySociete = keySociete;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPortable() {
        return portable;
    }

    public void setPortable(String portable) {
        this.portable = portable;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (keyPersonne != null ? keyPersonne.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 Personne)) {
            return false;
        }
        Personne other = (Personne) object;
        if ((this.keyPersonne == null && other.keyPersonne != null) || (this.keyPersonne != null && !this.keyPersonne.equals(other.keyPersonne))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "fr.ietevents.serviceweb.Entity.Personne[ keyPersonne=" + keyPersonne + " ]";
    }

}

1 个答案:

答案 0 :(得分:0)

CLIENT_IDclient的值为null。这就是你得到这个例外的原因。在将值传递给查询之前添加空检查。

希望这有帮助。