English 中文(简体)
How to configure JPA to use JNDI?
原标题:

I have a working persistence definition that works on java level tests. Now I want to incorporate that into a web application which defines the database connection as JNDI in the context.xml. What do I need to change to make it work with the JNDI instead of the persistence.xml or at least get the infor from there?

最佳回答

Your persistence.xml beginning should be something like this (using EclipseLink as implementation), for a jdbc/MYNAME JNDI name:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">    
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <non-jta-data-source>java:comp/env/jdbc/MYNAME</non-jta-data-source>
    <class>org.test.entity.MyEntity</class>
...
  </persistence-unit>
<persistence>

Of course you should set the configuration suitable for your environment. In the example I use a non-JTA DataSource: according to one of your comments, it seems your DataSource is not JTA-compliant. For Hibernate, the persistence provider should be different.

问题回答

you must use the persistence.xml when you re using JPA. because JPA must check the persistence.xml. so you can use persistence.xml and jndi at the same time.





相关问题
query must begin with SELECT or FROM: delete [delete

I am using JPA in addition to spring(3.0.0.M4). While deleting multiple records using query.executeUpdate() i am getting the following exception. org.springframework.web.util.NestedServletException: ...

Last update timestamp with JPA

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated. What are the strategies for making ...

@IdClass with non primative @Id

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes. class User { private long id; ... } class Token { private User user; private ...

Database not dropped in between unit test

Hello good people i came accross a weird behaviour in my test.I m using JPA hibernate annotation with spring. let say i have an Class MyObject and it s property email is marqued @Column(name="EMAIL", ...

Toplink trying to persist null object

I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null: java.lang.IllegalStateException: During ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签