如何用JPA进行注入?
是否可以使用 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
来不必担心管理 EntityMangerFactory
和 EntityManager
。
我正在使用 Eclipse Indigo Java EE Web Developers 和 GlassFish v3。
更新: 我遵循@Andrei Bodnarescu方法和这个教程 也可以通过 GF3 与我的数据库建立连接:
我更改了 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将事务类型更改为 JTA,如下所示 -
然后,如果您希望
Glassfish
来注入
,则可以按如下方式使用@PersistenceContext
EntityManager
到您的EJB
-您也可以在
Servlet
和ManagedBean
中执行。此外,在
Glassfish
服务器上,您可以创建JDBC 连接池
和JDBC 资源
,为其指定一个JNDI 名称
并使用它在persistence.xml
中声明您的EntityManager
,如下所示 -您可以从
Glassfish Admin 创建
JDBC 资源
-控制台。You have to change the transaction-type to JTA as follows -
And then you can use
@PersistenceContext
as follows if you wantGlassfish
toinject
theEntityManager
into yourEJB
-You can do in
Servlet
andManagedBean
too.Also, on your
Glassfish
server you can createJDBC Connection Pool
andJDBC Resource
, give it aJNDI name
and use it to declare yourEntityManager
in thepersistence.xml
as follows -You can create the
JDBC Resources
on from theGlassfish Admin-Console
.BheshG的回答非常好,这里只是一些小补充。
我认为为了激活 CDI 并因此进行依赖项注入,您需要创建一个空的 beans.xml 文件,您必须将其放置在项目的 WEB-INF 文件夹中
要在 GF3 中创建数据源并通过 JNDI 公开它(如 BheshG sais),您必须基本上这个:
资源->JDBC->JDBC连接池并制作连接池
创建将使用该池的数据源。
(我想发布带有连接池和数据源示例的图像,但我不能,因为我没有声誉点。为了不显得垃圾邮件和恶作剧,我只想说:如果您想要更多,请给我发电子邮件详细信息或示例项目,因为我现在正在编写一个教程)
现在您可以在 persistence.xml 文件中使用该数据源将其附加到持久性单元:
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:
resources->JDBC->JDBC Connection pools and make a connection pool
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: