JPA EntityManager null

时间:2018-06-01 23:52:21

标签: jpa nullpointerexception jboss jpql hibernate-entitymanager

我正在我的大学从事JBoss和JEE技术的项目,我遇到了EntityManager的问题。 实际上,我在persistence.xml中设置了持久化上下文 transaction-type =" JTA" 正如我在StackOverFlow中解决的许多教程和问题中发现的那样 我使用带有注释 @PersistenceContext 的EntityManager,其unitName与 persistence.xml 中的相同 在调用它时,它会抛出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());
    }
}

}

控制台输出:

service cree

实体经理为空

可空:

0 个答案:

没有答案