In persistence.xml I can declare the following :
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
Is it possible to catch this event and execute code ?
I am running on glassfish 3.1.
JPA 2.1 has standard named properties. javax.persistence.schema-generation.database.action should be set to "drop-and-create" and javax.persistence.sql-load-script-source can be set to the name of a script file to load data. These are standard so should be in the docs for your JPA provider.
Related
I m having a problem with ServiceMix and JPA.
I m new on servicemix and on the project I work for.
A bundle already exist to set the database configuration and contains a persistence.xml file with some properties in persistence-unit.
this persistent unit is for a postgresql database and work fine.
Recently someone add a new bundle. This bundle is a migration of an existing application that contains work to clean all table in relation with the treatment to have empty db -> readfile-> insert into db some data-> extract data into file for another application.
Now in OSGI bundle, we decide to create an H2 database with spring-context each time the process is launch. We work in memory and we don t have to clean all table with this solution.
So we have done a blueprint.xml that launch a bean, and the bean create a appContext = new OsgiBundleXmlApplicationContext(...)
And when treatment is done we do appContext.close() and all spring context is destroy. (this treatment is run only once a day so no problem with time loading)
To do so we have an applicationContext.xml that contain
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
...
<bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
....
....
All work fine, Spring Context is loaded with H2 in memory databse, and at the end of the treatment, h2 database not exist anymore.
Now the problem is when a query is launch on postgresql database, i have problem with insertion because entitymanager try to save entity with 0 id!
O id is meaning null because we don't allow-zero-id in persistence.xml for postgressql database, but in persistence.xml for H2 databse we set allow-zero-id to true and if the H2 treatment is launch only one time, the other entitymanager(postrgesql) don't work anymore and try to insert always 0 id in database instead of call sequence because of the allow-zero-id.
I don't now why the property for persistent unit defined in H2 databse ovveride property of persistence-unit of postgresql.
If someone can help? does new OsgiBundleXmlApplicationContext(..) try to find an osgi service for entitymaneger and get an existing one instead of create à new one?
I am new to Spring and going to develop a spring MVC Application.What is the best way to write root-context.xml for Bean class properties?
For database Connection I want to use Spring jdbc(JdbcTemplate).Can you please suggest me the best way to do so?
You're going to need to set up a DataSource and then create a JdbcTemplate bean that utilizes it. The Spring JDBC Reference Documentation provides examples and really good explanations on how to accomplish this.
Here's a basic bean definition for a datasource. The properties specified will depend on the database that you are using.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Then you can create a jdbcTemplate bean that uses the dataSource or you can instantiate the jdbcTemplate within your code.
The JdbcTemplate API mentions this:
Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference. Note: The DataSource should always be configured as a bean in the application context, in the first case given to the service directly, in the second case to the prepared template.
I have a code that works perfectly on WAS 7 but fail when i run it in WAS 8.0.0.5. I am using JPA 2.0 with openJPA as my provider. Calling persist on my em throws a nested exception. Has anyone ever managed to write a JPA program in WAS 8.0.0.5
here is the Exception
WTRN0074E: Exception caught from before_completion synchronization operation: org.apache.openjpa.persistence.PersistenceException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=.OPENJPA_SEQUENCE_TABLE, DRIVER=3.58.81 {prepstmnt -1559269434 SELECT SEQUENCE_VALUE FROM .OPENJPA_SEQUENCE_TABLE WHERE ID = ? FOR READ ONLY WITH RS USE AND KEEP UPDATE LOCKS [params=?]}
The SQLCODE=-204 points that something is missing. The log keeps printing THAKHANI.OPENJPA_SEQUENCE_TABLE which makes think that maybe the table is missing. You could also check to make sure the DB2 user that JPA is using has permissions to create tables and run SELECT statements on them.
I manage to resolve the problem by selecting Identity as my primary key generation mechanism when generating entities from tables. I also add the following in my persistence.xml.
<properties>
<!-- OpenJPA specific properties -->
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
<property name="openjpa.jdbc.DBDictionary" value="db2"/>
<property name="openjpa.jdbc.Schema" value=<SchemaName>/>
</properties>
In my application we are using Toplink with Jpa.
Here the problem is we are using stored procedures in this application, we are taking the connection using Jndi connection for Stored Procedure calling, and we are using EntityManger for remaining queries. But here if we launching the application it is taking two connections from connection pool. After the application launching I am calling the Stored Procedure(sp)
one sp I am taking one connection but in websphere connection pool it is creating two connections?
can U plese help me how to overcome this problem.....
I won't using JTA, to get the JDBC connection I am using
EntityManager em = getJpaTemplate().getEntityManagerFactory().createEntityManager();
this way I am getting the JDBC Connection...and I configured the persitence.xml file following code...
<properties>
<property name="toplink.logging.level" value="OFF"/>
<property name="toplink.cache.type.default" value="NONE"/>
<property name="com.thoughtinc.runtime.persistence.sql.syntax" value="db2" />
</properties>
So, please kindly look into this and tell me any if I am doing any wrong here.
Are you using JTA or non-JTA? Are you releasing the connection back to the pool after using it?
Depending on your configuration (include your persistence.xml), if you have a non-JTA login configured TopLink may use this for non-transactional read queries. This is configurable in your persistence.xml.
To get the JDBC Connection from a TopLink (EclipseLink) EntityManager use em.unwrap(Connection.class)
I have a perfectly working application client deployed to a GlassFish v2 server inside an ear with some EJBs, Entities, etc. I'm using eclipselink.
Currently I have in my persistence.xml:
<persistence-unit name="mysource">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/mysource</jta-data-source>
<class>entities.one</class>
<class>entities.two</class>
...
<properties>
<property name="eclipselink.target-server" value="SunAS9"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
And this works fine when I inject the EntityManager into the EJB:
#PersistenceContext(unitName="mysource")
private EntityManager em;
Now I have a requirement to dynamically switch persistence units/databases.
I figure I can get an EntityManager programatically:
em = Persistence.createEntityManagerFactory("mysource").createEntityManager();
but I get the following error:
Unable to acquire a connection from driver [null], user [null] and URL [null]
Even "overriding" javax.persistence.jtaDataSource" to "jdbc/mysource" in a Map and calling createEntityManagerFactory("mysource", map) doesn't make a difference.
What am I missing?
You are trying to circumvent the container with creating an entity manager programmatically and this means you'll most probably create a non-JTA data source (as it's outside the container, the transaction type should be RESOURCE_LOCAL), thus your original config is useless.
Try injecting an entity manager with a different unitName property or create a RESOURCE_LOCAL transaction type persistence unit.