如何用JPA进行注入?

发布于 2024-12-03 16:59:10 字数 2722 浏览 1 评论 0原文

是否可以使用 EclipseLink 2.3 在 EntityManager 中进行注入?

这是我的 persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="SuaParte" transaction-type="RESOURCE_LOCAL">
                // classes..
        <properties>
            <property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/schema"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

我是 JPA 新手,所以我最初创建这个 persistence.xml 文件只是为了测试它是否有效,但现在我想使用 @PersistenceContext 来不必担心管理 EntityMangerFactoryEntityManager

我正在使用 Eclipse Indigo Java EE Web Developers 和 GlassFish v3。

更新: 我遵循@Andrei Bodnarescu方法和这个教程 也可以通过 GF3 与我的数据库建立连接:

Ping database

我更改了 persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="SuaParte" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/mysql</jta-data-source>
                //classes.. 
    </persistence-unit>
</persistence>

所以我尝试在数据库中保留某些内容:

@Stateless
@LocalBean
public class DaoUser {
    @PersistenceContext(unitName="SuaParte")
    private EntityManager em;

    public void persist(User user){
        try{
            em.persist(user);
            }catch(Exception e){
            e.printStackTrace();
        }
    }
}

它返回一个 em.persist(user); 中的 java.lang.NullPointerException。 我做错了什么?

Is possible do injection in EntityManager with EclipseLink 2.3 ?

This is my persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="SuaParte" transaction-type="RESOURCE_LOCAL">
                // classes..
        <properties>
            <property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/schema"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

I'm new with JPA so I created this persistence.xml file at first just to test to see if it works, but now I would like to use @PersistenceContext to don't have to worry about manage the EntityMangerFactory and EntityManager.

I'm using Eclipse Indigo Java EE Web Developers with GlassFish v3.

UPDATE:
I follow @Andrei Bodnarescu approach and this tutorial too and everything is fine to get a connection with my database through GF3:

Ping database

And i change my persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="SuaParte" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/mysql</jta-data-source>
                //classes.. 
    </persistence-unit>
</persistence>

So i try to persist something in my database:

@Stateless
@LocalBean
public class DaoUser {
    @PersistenceContext(unitName="SuaParte")
    private EntityManager em;

    public void persist(User user){
        try{
            em.persist(user);
            }catch(Exception e){
            e.printStackTrace();
        }
    }
}

And it returns a java.lang.NullPointerException in em.persist(user);.
What am I doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-12-10 16:59:10

您必须将事务类型更改为 JTA,如下所示 -

<persistence-unit name="SuaParte" transaction-type="JTA">

然后,如果您希望 Glassfish注入,则可以按如下方式使用 @PersistenceContext EntityManager 到您的 EJB -

@Stateless
@LocalBean
public class MyEjb {

    @PersistenceContext("SuaParte")
    private EntityManager suaParteEM; 

}

您也可以在 ServletManagedBean 中执行。

此外,在 Glassfish 服务器上,您可以创建 JDBC 连接池JDBC 资源,为其指定一个 JNDI 名称 并使用它在 persistence.xml 中声明您的 EntityManager,如下所示 -

<persistence-unit name="SuaParte" transaction-type="JTA">
    <jta-data-source>JNDI_Name_Of_JDBC_Resource</jta-data-source>

您可以从 Glassfish Admin 创建 JDBC 资源 -控制台。

You have to change the transaction-type to JTA as follows -

<persistence-unit name="SuaParte" transaction-type="JTA">

And then you can use @PersistenceContext as follows if you want Glassfish to inject the EntityManager into your EJB -

@Stateless
@LocalBean
public class MyEjb {

    @PersistenceContext("SuaParte")
    private EntityManager suaParteEM; 

}

You can do in Servlet and ManagedBean too.

Also, on your Glassfish server you can create JDBC Connection Pool and JDBC Resource, give it a JNDI name and use it to declare your EntityManager in the persistence.xml as follows -

<persistence-unit name="SuaParte" transaction-type="JTA">
    <jta-data-source>JNDI_Name_Of_JDBC_Resource</jta-data-source>

You can create the JDBC Resources on from the Glassfish Admin-Console.

傾旎 2024-12-10 16:59:10

BheshG的回答非常好,这里只是一些小补充。

  • 我认为为了激活 CDI 并因此进行依赖项注入,您需要创建一个空的 beans.xml 文件,您必须将其放置在项目的 WEB-INF 文件夹中

  • 要在 GF3 中创建数据源并通过 JNDI 公开它(如 BheshG sais),您必须基本上这个:

    1. 资源->JDBC->JDBC连接池并制作连接池

    2. 创建将使用该池的数据源。

(我想发布带有连接池和数据源示例的图像,但我不能,因为我没有声誉点。为了不显得垃圾邮件和恶作剧,我只想说:如果您想要更多,请给我发电子邮件详细信息或示例项目,因为我现在正在编写一个教程)

现在您可以在 persistence.xml 文件中使用该数据源将其附加到持久性单元:

    <persistence-unit name="emJTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/postgre_ds_jta</jta-data-source>
    <mapping-file>META-INF/emp-mappings.xml</mapping-file>
    <class>model.Employee</class>
    <class>model.ProjectManager</class>
    <class>model.Department</class>
    <class>model.Project</class>
    <class>model.Phone</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>       
</persistence-unit>

BheshG's answer is very good, here's just some small addendums.

  • i think that in order to activate CDI and thus have dependency injection you need to create an empty beans.xml file that you must place in the WEB-INF folder of your project

  • To create a data source in GF3 and expose it via JNDI like BheshG sais, you must to basically this:

    1. resources->JDBC->JDBC Connection pools and make a connection pool

    2. Create a datasource that will use the pool.

(I wanted to post images with examples for conenction pool and datasource, but I can't since I don't have reputation points. In order to not seem spammy and trolly, I'm just gonna say: email me if you want more details or an example project, as I'm working on a tutorial on exactly that right now)

Now you can use that datasource in the persistence.xml file to attach it to a persistence unit:

    <persistence-unit name="emJTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/postgre_ds_jta</jta-data-source>
    <mapping-file>META-INF/emp-mappings.xml</mapping-file>
    <class>model.Employee</class>
    <class>model.ProjectManager</class>
    <class>model.Department</class>
    <class>model.Project</class>
    <class>model.Phone</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>       
</persistence-unit>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文