我正在我的大学开展一个关于JBoss和JEE技术的项目,我遇到了EntityManager的问题 . 实际上,我在persistence.xml中使用 transaction-type="JTA" 设置了持久化上下文,因为我在许多教程中发现它并且在StackOverFlow中解决问题我使用带有注释 @PersistenceContext 的EntityManager,其中unitName与_2797319中的相同_当调用它时,它会抛出一个NullPointerException,我发现了EntityManager导致的错误 . 我在这里附上了实体的代码块,持久性,服务和对方法的调用
persistence.xml中
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="examen-ejb" transaction-type="JTA">
<jta-data-source>java:/Examen_DS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</properties>
</persistence-unit>
</persistence>
Articles.java
public class Articles implements InterfaceArticles{
@PersistenceContext(unitName="examen-ejb")
private EntityManager em ;
@Override
public List<Article> getAll() {
List<Article> all = null;
TypedQuery<Article> query ;
if ( em != null)
{
query = em.createQuery("SELECT a FROM Article a", Article.class);
all = query.getResultList() ;
System.out.println("empty");
System.out.println(all.isEmpty());
}
else
{
System.out.println("entity manager is null");
}
System.out.println("nullable : ");
System.out.println(all == null);
return all;
}
}
Article.java
@Entity
public class Article implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int identifiant ;
private String libelle ;
private String description ;
private Categories category ;
private Type type ;
private double prix ;
@OneToMany(mappedBy="article", fetch=FetchType.EAGER)
private List<CompoArticle> compositions ;
@OneToMany (mappedBy="article", fetch=FetchType.EAGER)
private List<Commande> commandes ;
public int getIdentifiant() {
return identifiant;
}
public void setIdentifiant(int identifiant) {
this.identifiant = identifiant;
}
public String getLibelle() {
return libelle;
}
public void setLibelle(String libelle) {
this.libelle = libelle;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Categories getCategory() {
return category;
}
public void setCategory(Categories category) {
this.category = category;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public double getPrix() {
return prix;
}
public void setPrix(double prix) {
this.prix = prix;
}
public List<Commande> getCommandes() {
return commandes;
}
public void setCommandes(List<Commande> commandes) {
this.commandes = commandes;
}
public List<CompoArticle> getCompositions() {
return compositions;
}
public void setCompositions(List<CompoArticle> compositions) {
this.compositions = compositions;
}
}
Test.java
public class Tests {
public static void main(String[] args)
{
Articles serv = new Articles() ;
System.out.println("service cree");
List<Article> list = serv.getAll() ;
if (list != null )
{
for (Article art : list)
System.out.println(art.toString());
}
}
}
控制台输出:
服务中心
实体管理器为空
可空的:
真正