命名查询和 POJO class
NamedQuery in POJO class
这是我的实体:
Client
import java.io.Serializable;
import java.util.List;
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.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author AMINA
*/
@Entity
@Table(name = "client")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"),
@NamedQuery(name = "Client.findById", query = "SELECT c FROM Client c WHERE c.id = :id"),
@NamedQuery(name = "Client.findByPr\u00e9nom", query = "SELECT c FROM Client c WHERE c.pr\u00e9nom = :pr\u00e9nom"),
@NamedQuery(name = "Client.findByNom", query = "SELECT c FROM Client c WHERE c.nom = :nom"),
@NamedQuery(name = "Client.findByT\u00e9l\u00e9phone", query = "SELECT c FROM Client c WHERE c.t\u00e9l\u00e9phone = :t\u00e9l\u00e9phone"),
@NamedQuery(name = "Client.findByEmail", query = "SELECT c FROM Client c WHERE c.email = :email")})
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "pr\u00e9nom")
private String prénom;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "nom")
private String nom;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 13)
@Column(name = "t\u00e9l\u00e9phone")
private String téléphone;
// @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
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "email")
private String email;
//liste des commandes d'un client
@OneToMany (mappedBy = "c")
public List<Commande> commandes;
public Client() {
}
public Client(Integer id) {
this.id = id;
}
public Client(Integer id, String prénom, String nom, String téléphone, String email) {
this.id = id;
this.prénom = prénom;
this.nom = nom;
this.téléphone = téléphone;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPrénom() {
return prénom;
}
public void setPrénom(String prénom) {
this.prénom = prénom;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getTéléphone() {
return téléphone;
}
public void setTéléphone(String téléphone) {
this.téléphone = téléphone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Client)) {
return false;
}
Client other = (Client) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.mycompany.exempletd.Client[ id=" + id + " ]";
}
}
指挥官
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.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author AMINA
*/
@Entity
@Table(name = "commande")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Commande.findAll", query = "SELECT c FROM Commande c"),
@NamedQuery(name = "Commande.findById", query = "SELECT c FROM Commande c WHERE c.id = :id")})
public class Commande implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@ManyToOne
private Client c;
public Commande() {
}
public Commande(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Commande)) {
return false;
}
Commande other = (Commande) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.mycompany.exempletd.Commande[ id=" + id + " ]";
}
}
class哪里有问题
import java.util.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.NamedQuery;
@NamedQuery(name = "reqSelect", query = "Select c.commandes From client c where c.nom like :name"); //problem
public class SaisieCommande {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.mycompany_ExempleTd_war_1.0-SNAPSHOTPU");
static EntityManager em = emf.createEntityManager();
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
Scanner s = null;
List commandes;
//saisie de nom
System.out.println("donner un nom");
String nom = s.next();
//saisie la methode
System.out.println("requête statique 1 requêtes dynamique 2");
int choix = s.nextInt();
if (choix == 1) {
commandes = em.createNamedQuery("reqSelect").setParameter("name", nom).getResultList();
} else if (choix == 2) {
commandes = em.createQuery("Select c.commandes From client c where c.nom like :name").setParameter("name",nom).getResultList();
}
}
}
我想让应用程序 Java 显示名称
所有客户端命令
我写了这篇文章,但命名查询有问题。我是 Java EE 的初学者。
@NamedQuery
只能用在类中,也就是@Entity
。只需将您的命名查询从 SaisieCommande
移动到您的任何实体。
这是我的实体:
Client
import java.io.Serializable;
import java.util.List;
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.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author AMINA
*/
@Entity
@Table(name = "client")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"),
@NamedQuery(name = "Client.findById", query = "SELECT c FROM Client c WHERE c.id = :id"),
@NamedQuery(name = "Client.findByPr\u00e9nom", query = "SELECT c FROM Client c WHERE c.pr\u00e9nom = :pr\u00e9nom"),
@NamedQuery(name = "Client.findByNom", query = "SELECT c FROM Client c WHERE c.nom = :nom"),
@NamedQuery(name = "Client.findByT\u00e9l\u00e9phone", query = "SELECT c FROM Client c WHERE c.t\u00e9l\u00e9phone = :t\u00e9l\u00e9phone"),
@NamedQuery(name = "Client.findByEmail", query = "SELECT c FROM Client c WHERE c.email = :email")})
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "pr\u00e9nom")
private String prénom;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "nom")
private String nom;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 13)
@Column(name = "t\u00e9l\u00e9phone")
private String téléphone;
// @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
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "email")
private String email;
//liste des commandes d'un client
@OneToMany (mappedBy = "c")
public List<Commande> commandes;
public Client() {
}
public Client(Integer id) {
this.id = id;
}
public Client(Integer id, String prénom, String nom, String téléphone, String email) {
this.id = id;
this.prénom = prénom;
this.nom = nom;
this.téléphone = téléphone;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPrénom() {
return prénom;
}
public void setPrénom(String prénom) {
this.prénom = prénom;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getTéléphone() {
return téléphone;
}
public void setTéléphone(String téléphone) {
this.téléphone = téléphone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Client)) {
return false;
}
Client other = (Client) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.mycompany.exempletd.Client[ id=" + id + " ]";
}
}
指挥官
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.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author AMINA
*/
@Entity
@Table(name = "commande")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Commande.findAll", query = "SELECT c FROM Commande c"),
@NamedQuery(name = "Commande.findById", query = "SELECT c FROM Commande c WHERE c.id = :id")})
public class Commande implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@ManyToOne
private Client c;
public Commande() {
}
public Commande(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Commande)) {
return false;
}
Commande other = (Commande) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.mycompany.exempletd.Commande[ id=" + id + " ]";
}
}
class哪里有问题
import java.util.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.NamedQuery;
@NamedQuery(name = "reqSelect", query = "Select c.commandes From client c where c.nom like :name"); //problem
public class SaisieCommande {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.mycompany_ExempleTd_war_1.0-SNAPSHOTPU");
static EntityManager em = emf.createEntityManager();
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
Scanner s = null;
List commandes;
//saisie de nom
System.out.println("donner un nom");
String nom = s.next();
//saisie la methode
System.out.println("requête statique 1 requêtes dynamique 2");
int choix = s.nextInt();
if (choix == 1) {
commandes = em.createNamedQuery("reqSelect").setParameter("name", nom).getResultList();
} else if (choix == 2) {
commandes = em.createQuery("Select c.commandes From client c where c.nom like :name").setParameter("name",nom).getResultList();
}
}
}
我想让应用程序 Java 显示名称 所有客户端命令
我写了这篇文章,但命名查询有问题。我是 Java EE 的初学者。
@NamedQuery
只能用在类中,也就是@Entity
。只需将您的命名查询从 SaisieCommande
移动到您的任何实体。