sample to use jpa in siwpas

14 views
Skip to first unread message

fiorenzino

unread,
Oct 24, 2010, 11:13:06 AM10/24/10
to SIwpas Discussion
Hi,

Can someone provide an example of how to declare a datasource and use
JPA in a web project like siwpas-sample-collapse?

Using a presistence.xml like this:

<?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="default" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</
provider>

<class>siwpas.sample.par.Person</class>


<properties>
<property name="openjpa.jdbc.DBDictionary"
value="mysql(SupportsSubselect=true)" />
<property name="openjpa.ConnectionDriverName"
value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionURL" value="jdbc:mysql://
localhost/openjpa" />
<property name="openjpa.ConnectionUserName" value="openjpa" />
<property name="openjpa.ConnectionPassword" value="openjpa" />
<property name="openjpa.Log" value="DefaultLevel=TRACE" />
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
</properties>

</persistence-unit>
</persistence>

the open-jpa plugin start in siwpas, but the entitiy manager give an
error like this:

http://mail-archives.apache.org/mod_mbox/openjpa-users/200907.mbox/%3C124648310551...@n2.nabble.com%3E

I also tried to use a datasource declaration in 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="default" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</
provider>
<jta-data-source>default</jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>

and using openejb.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Resource id="default" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://localhost/openjpa
UserName openjpa
Password openjpa
</Resource>

but it don't works!!

Soma ideas?

thanks in advance

fiorenzo

fiorenzino

unread,
Oct 24, 2010, 12:04:34 PM10/24/10
to SIwpas Discussion
some progress:

using this persistence.xml:
<persistence-unit name="test" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</
provider>
<jta-data-source>testTRANS</jta-data-source>
<non-jta-data-source>testNOTRANS</non-jta-data-source>

<properties>
<property name="openjpa.Log" value="DefaultLevel=TRACE" />
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>

and adding in siwpas-1.0.0-CR4/conf/openejb.xml my personal
datasource:

<Resource id="testTRANS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://localhost:3306/openjpa
UserName openjpa
Password openjpa
</Resource>
<Resource id="testNOTRANS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://localhost:3306/openjpa
UserName openjpa
Password openjpa
JtaManaged false
</Resource>

IT WORKS!!!

But some question remains:
1) can i add datasource dynamically (in a similar way as is done eg.
in jboss datasource-ds.xml) with xml file?
2) where must i define openejb.xml (in webbapp.war/WEB-INF/classes/
META-INF or in webbapp.war/META-INF?

bye

fiorenzo

On 24 Ott, 17:13, fiorenzino <fiorenz...@gmail.com> wrote:
> Hi,
>
> Can someone provide an example of how to declare a datasource and use
> JPA in a web project like siwpas-sample-collapse?
>
> Using a presistence.xml like this:
>
> <?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/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
>         version="1.0">
>         <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
>                 <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</
> provider>
>
>                 <class>siwpas.sample.par.Person</class>
>
>                 <properties>
>                         <property name="openjpa.jdbc.DBDictionary"
> value="mysql(SupportsSubselect=true)" />
>                         <property name="openjpa.ConnectionDriverName"
> value="com.mysql.jdbc.Driver" />
>                         <property name="openjpa.ConnectionURL" value="jdbc:mysql://
> localhost/openjpa" />
>                         <property name="openjpa.ConnectionUserName" value="openjpa" />
>                         <property name="openjpa.ConnectionPassword" value="openjpa" />
>                         <property name="openjpa.Log" value="DefaultLevel=TRACE" />
>                         <property name="openjpa.jdbc.SynchronizeMappings"
> value="buildSchema(ForeignKeys=true)" />
>                 </properties>
>
>         </persistence-unit>
> </persistence>
>
> the open-jpa plugin start in siwpas, but the entitiy manager give an
> error like this:
>
> http://mail-archives.apache.org/mod_mbox/openjpa-users/200907.mbox/%3C1246483105514-3191867.p...@n2.nabble.com%3E
>
> I also tried to use a datasource declaration in 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/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
Reply all
Reply to author
Forward
0 new messages