File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes How database info is specfied in persistence.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How database info is specfied in persistence.xml" Watch "How database info is specfied in persistence.xml" New topic
Author

How database info is specfied in persistence.xml

Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
Hi,

Can anyone please tell me what are the things that need to specified in the persistence.xml, if i am using Oracle 9i as my Database.

Thanks
Adam


---------------------------------<br />Thanks,<br />Richard
Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
Hi Mark,

Following is the persistence.xml file given in chapter 2 of the book.

<persistence>
<persistence-unit name="EmployeeService" transaction- type="RESOURCE_LOCAL">
<class>examples.model.Employee</class>
<properties>
<property name="toplink.jdbc.driver"
value="org.apache.derby.jdbc.ClientDriver"/>
<property name="toplink.jdbc.url"
value="jdbc erby://localhost:1527/EmpServDB;create=true"/>
<property name="toplink.jdbc.user" value="APP"/>
<property name="toplink.jdbc.password" value="APP"/>
</properties>
</persistence-unit>
</persistence>

I am using a Oracle 9i Database. Is the following persistence.xml correct

<persistence>
<persistence-unit name="EmployeeService" transaction- type="RESOURCE_LOCAL">
<class>examples.model.Employee</class>
<properties>
<property name="toplink.jdbc.driver"
value="oracle.jdbc.driver.OracleDriver"/>
<property name="toplink.jdbc.url"
value="jdbc racle:thin:@162.44.9.87:1521:ZEUSI18N"/>
<property name="toplink.jdbc.user" value="swat4drs"/>
<property name="toplink.jdbc.password" value="swat4drs"/>
</properties>
</persistence-unit>
</persistence>

Are the values given for the name attribute correct. Do I need to specify the Persistence provider name in the <provider></provider>.
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Adam,

I would also add in the db platform property:

<property name="toplink.target-database" value="Oracle"/>

In theory you should not need to specify the provider when there is only one on the classpath when running in SE. (If this is not working for you right now then you should download the latest version of TopLink Essentials from the Glassfish.) It never hurts to put it in, though. You would simply include:

<provider>oracle.toplink.essentials.PersistenceProvider</provider>

Take a look at the example in the book for the actual XML file header. In its completeness it would look something like:

<persistence 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_1_0.xsd"
version="1.0">

(It looks like you may also have an extra space in your transaction-type element.)

-Mike


-Mike
Pro JPA 2: Mastering the Java Persistence API
Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
Hi Mike,

I have configured the persistence.xml as you have said, i was able to deploy the application successfully, but when I am running my client the continer was not able to find the table in the Database, eventhough i have created the Table in the right schema and provided the right credentials to connect to that schema in the persistence.xml. Does the container look for the DB credentials anywhere else.
Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
Following is the exception that i got:

Internal Exception: org.apache.derby.client.am.SqlException: Table 'ACCOUNT' does not exist.Error Code: -1
Call:SELECT ID, OWNERNAME, BALANCE FROM ACCOUNT
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Ummm, that would be because the table doesn't exist. ;-)
If you want to generate the db schema then you need to specify that in the properties section as well.

<property name="toplink.ddl-generation" value="drop-and-create-tables"/>

-Mike
Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
Hey Mike,

As you have said I included the <property> tag in the persistence.xml, I got rid of that exception but I was not able to find any data in the tables. In the client code I do a create and then try to find the data that I created, it throws an EJBException as it was not able to find the data.

Moreover when I remove the finder and have the create code alone, it says that the record was created, I confirm this as manager.contains() returned true. But when I queried in the DB directly I was not able to find the data. So, I believe that the record is created in some other instance. The credentials that I have given in the persistence.xml to connect to the DB are correct, I have verified them again and again. I have also created a Datasource in the Application Server which is the same as the one given in the persistence.xml. I am not sure what I am missing here, where else would the data be created, were else would it find the credentials to connect to DB.
Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
When I changed the credentials to wrong values given in the persistence.xml, it still goes and creates the data. I really confused from where it is getting the DB information. Please help me out.
Alexandros Stivaktatis
Greenhorn

Joined: Aug 15, 2006
Posts: 7
Hi,

Trying to help

Are you sure you have only one persistence.xml?

Do you have another property file like for example
'xyz.properties' that is beeing checked by your app?

Bye,

Alex
Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
I do have only one persistence.xml, and i dont have any properties file too. I am wondering if it is taking any info from the application server, i am not sure though.
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Adam,

You also need to start and commit a transaction in order for the data to actually be persisted. I recommend that you take a look at the book sample code on the Apress site. It will likely help you to get started.

-Mike
Alexandros Stivaktatis
Greenhorn

Joined: Aug 15, 2006
Posts: 7
Are you sure your DataSource isn't used to make
this connection, or possibly there is still a
default datasource that is being used?

Bye, good luck

Alex
Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
I just looked into the samples of the book, so I changed my code to begin and commit transactions. Everything is working fine now, I have no exceptions, in the console I am able to see the correct outputs in my console, but the point is that I don't know where all these data are been written to. I did change my DB connection Info to wrong values in both persistence.xml and in the in the Application Server, but still everything is going fine I did not get any errors. I am using Sun Java Application Server is there anything like default DB connection available in this server.
Richard Collins
Greenhorn

Joined: Aug 15, 2006
Posts: 18
There is a default server in SJAS 9.0 called JavaDB, i believe all the data that i create goes into that DB, I am not sure about this though. While creating the EntityManager I was creating it using the @PerssitenceContext annotation, is it because of that the value are getting written in JavaDB.

I changed the code to get the EntityManager from EntityManagerFactory. The code goes like this,

EntityManagerFactory emf = Persistence.createEntityManagerFactory("AccountService");
manager = emf.createEntityManager();

This code is in a Session bean.

Following is my persistence.xml.

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd" version="1.0">
<persistence-unit name="AccountService" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>sample.entity.account.Account</class>
<properties>
<property name="toplink.target-database" value="Oracle"/>
<property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="toplink.jdbc.url" value="jdbc racle:thin:@zeus1dev:1521:zeusdev"/>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
<property name="toplink.jdbc.user" value="olga_twe48_lease"/>
<property name="toplink.jdbc.password" value="olga_twe48_lease"/>
</properties>
</persistence-unit>
</persistence>


I have this persistence.xml file inside META-INF folder. When I do this i get the following error message.

Caused by: Exception [TOPLINK-7189] (Oracle TopLink Essentials - 2006.4 (Build 060412)): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: Cannot create temp classloader from current loader: [EJBClassLoader :
urlSet = [URLEntry : file:/C:/Sun/AppServer/domains/domain1/applications/j2ee-modules/entity/, URLEntry : file:/C:/Sun/AppServer/domains/domain1/generated/ejb/j2ee-modules/entity/]
doneCalled = false
Parent -> EJBClassLoader :
urlSet = []
doneCalled = false
Parent -> java.net.URLClassLoader@113beb5



Any of you guys know what this error message means.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How database info is specfied in persistence.xml