How to Initialize persistance.xml variables on program start? - java

Basically I am doing a Insert inside a PostgreSQL database, but since I have multiple databases I have to change the configurations based on each environment.
The thing is that I used a not really good way to do so, since everytime I do a insert, I replace all variables, which is ultra slow and definetly not a good way to achiev that.
How would I be able to do the same inserts, but having it configured while my Api started?
Here's what i'm currently doing:
package br.jus.tjba.dje.local.service;
import br.jus.tjba.dje.local.entity.Conteudo;
import br.jus.tjba.dje.local.repository.ConteudoRepository;
import br.jus.tjba.tjfw4.core.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.persistence.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
#Service
public class ConteudoService extends AbstractService {
#Autowired
private ConteudoRepository conteudoRepository;
public EntityTransaction insertInDb(String something, String somethingElse) {
Map<String, String> env = System.getenv();
Map<String, Object> configOverrides = new HashMap<>();
// Here I change my persistance.xml configs
for(Map.Entry<String, String> entry: env.entrySet()) {
if (entry.getKey().contains("DATABASE_URL")) {
configOverrides.put("javax.persistence.jdbc.url", env.get(entry.getValue()));
} else if (entry.getKey().contains("DATABASE_USER")) {
configOverrides.put("javax.persistence.jdbc.user", env.get(entry.getValue()));
} else if (entry.getKey().contains("DATABASE_PASSWORD")) {
configOverrides.put("javax.persistence.jdbc.password", env.get(entry.getValue()));
}
}
// Cria um factory dando override nas variáveis.
EntityManagerFactory factory = Persistence.createEntityManagerFactory("default", configOverrides);
EntityManager conteudoManager = factory.createEntityManager();
Query query = conteudoManager.createNativeQuery("INSERT INTO something(" +
"SOMETHING," +
"SOMETHING_ELSE)" +
" VALUES (" +
":something," +
":somethingElse)");
conteudoManager.getTransaction().begin();
query.setParameter("something", something);
query.setParameter("somethingElse", somethingElse);
query.executeUpdate();
return conteudoManager.getTransaction();
}
public Conteudo getContent() {
return conteudoRepository.getContent();
}
}
Also my persistance.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="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="javax.persistence.jdbc.url" value="url" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="javax.persistence.jdbc.user" value="login" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>

Related

Serverless framework with java MySQL conexion

I'm new with serverless framework. I'm trying to make a test of a simple application with maven, spring, hibernate, jpa, mysql and serverless.
this is my project
As a restApi works ok, but when I invoke the function in serverless terminal the information came null, because is not getting the info from the db.
this is my code
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_2_0.xsd"
version="2.0">
<persistence-unit name="serverless-java" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.serverless.persistence.Wizard</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://mydb.uiyfuas7434.us-east-1.rds.amazonaws.com:3306/mydb" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="1234" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!-- Important -->
<property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="10" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="120" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="240" />
<!-- Features -->
<property name="hibernate.connection.autoReconnect" value = "true"/>
<property name="hibernate.show_sql" value = "false" />
<property name="hibernate.connection.autoReconnectForPools" value = "true"/>
<property name="hibernate.connection.is-connection-validation-required" value = "true"/>
</properties>
</persistence-unit>
</persistence>
my Handler class
public class Handler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {
private static final Logger logger = LoggerFactory.getLogger(TestService.class);
#Autowired
private IBaseService service;
#Override
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
List<Wizard> prueba = service.getWizards();
Map<String, Wizard> response = new HashMap<>();
Response responseBody = new Response("success", response);
try {
for (Wizard entity : prueba) {
response.put(entity.getName(), entity);
}
responseBody.setInput(response);
} catch (Exception e) {
logger.error(e.getMessage(), e);
responseBody.setMessage("Fail: " + e.getMessage());
}
return ApiGatewayResponse.builder().setStatusCode(200).setObjectBody(responseBody)
.setHeaders(Collections.singletonMap("This is a test", "AWS Lambda & serverless")).build();
}
}
my serverless.yml
service: aws-java-maven # NOTE: update this with your service name
provider:
name: aws
runtime: java8
iamRoleStatements:
- Effect: "Allow"
Action:
- "mysql:*"
Resource: "*"
package:
artifact: target/hello-dev.jar
functions:
get-transactions:
handler: com.serverless.Handler

Java hibernate JPA error with JUnit Testing

I am using hibernate in my Java EE application in combination with my Wildfly server to persist my Classes in a mysql Database.
So far this works fine but now I am writing unit tests and I am getting crazy about some error which I get.
I would like to test my DAO-Layer in my Unit-Tests but I get these errors:
Caused by: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [java:/MySqlDS]
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
my persistence.xml ist this:
<?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="primary">
<jta-data-source>java:/MySqlDS</jta-data-source>
<class>org.se.bac.data.Employee</class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/empdb?useSSL=false"/>
<property name="hibernate.connection.username" value="student"/>
<property name="hibernate.connection.password" value="student"/>
<!--
SQL stdout logging
-->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
So, Here I am using a jta-data-source> as you can see.
If I remove this line my tests are going fine! But I can not build my project with maven anymore.
Error:
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.jdbc.Driver]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver"}}
He can not find the datasource because I removed the line in my persistence.xml
How can I manage to get both run in my application. The tests and of course the maven build?
Here is my test: (Setup is already causing the error):
package org.se.bac.data.dao;
import java.util.List;
import javax.persistence.EntityManager;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.se.bac.data.model.Employee;
public class EmployeeDAOTest
{
private static final JdbcTestHelper JDBC_HELPER = new JdbcTestHelper();
private final static JpaTestHelper JPA_HELPER = new JpaTestHelper();
private EntityManager em = JPA_HELPER.getEntityManager("primary");
private EmpDAO dao;
#BeforeClass
public static void init()
{
JDBC_HELPER.executeSqlScript("sql/test/dropEmployeeTable.sql");
JDBC_HELPER.executeSqlScript("sql/test/createEmployeeTable.sql");
}
#AfterClass
public static void destroy()
{
//JDBC_HELPER.executeSqlScript("sql/test/dropEmployeeTable.sql");
}
#Before
public void setUp()
{
JDBC_HELPER.executeSqlScript("sql/test/dropEmployeeTable.sql");
JDBC_HELPER.executeSqlScript("sql/test/createEmployeeTable.sql");
dao = new EmpDAOImpl();
dao.setEm(em);
JPA_HELPER.txBegin();
Employee emp2 = new Employee();
emp2.setFirstname("Max");
emp2.setLastname("Muster");
emp2.setHiredate("23-12-1991");
dao.insert(emp2);
}
And JPAHELPER Class:
package org.se.bac.data.dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaTestHelper
{
/*
* Property: persistenceUnitName
*/
private String persistenceUnitName;
public String getPersistenceUnitName()
{
return persistenceUnitName;
}
public void setPersistenceUnitName(String persistenceUnitName)
{
if(persistenceUnitName == null || persistenceUnitName.length() == 0)
throw new IllegalArgumentException("Illegal parameter persistenceUnitName = " + persistenceUnitName);
this.persistenceUnitName = persistenceUnitName;
}
/*
* Get an instance of the EntityManagerFactory.
*/
protected EntityManagerFactory getEnityManagerFactory()
{
if(persistenceUnitName == null)
throw new IllegalStateException("PersistenceUnitName must be set!");
return Persistence.createEntityManagerFactory(persistenceUnitName);
}
/*
* Manage an EntityManager.
*/
private EntityManager em;
public EntityManager getEntityManager()
{
if(em == null)
{
em = getEnityManagerFactory().createEntityManager();
}
return em;
}
public EntityManager getEntityManager(String persistenceUnitName)
{
setPersistenceUnitName(persistenceUnitName);
return getEntityManager();
}
public void closeEntityManager()
{
if(em != null)
em.close();
}
/*
* Handle Transactions
*/
protected void txBegin()
{
EntityTransaction tx = em.getTransaction();
tx.begin();
}
protected void txCommit()
{
EntityTransaction tx = em.getTransaction();
if(tx.getRollbackOnly())
{
tx.rollback();
}
else
{
tx.commit();
}
}
protected void txRollback()
{
EntityTransaction tx = em.getTransaction();
tx.rollback();
}
}
and my DAO:
package org.se.bac.data.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.se.bac.data.model.Employee;
class EmpDAOImpl // package private
implements EmpDAO
{
#PersistenceContext
private EntityManager em;
/*
* CRUD methods
*/
public Employee findById(int id)
{
System.out.println("empdaoimpl ID " + id);
return em.find(Employee.class, id);
}
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
}
Wildfly Datasource:
<datasources>
<datasource jta="true" jndi-name="java:/MySqlDS" pool-name="MySqlDS" enabled="true" use-ccm="false">
<connection-url>jdbc:mysql://localhost:3306/empdb?useSSL=false</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql-connector-java-5.1.44-bin.jar_com.mysql.jdbc.Driver_5_1</driver>
<security>
<user-name>student</user-name>
<password>student</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
</datasource>
</datasources>
He can not find the datasource because I removed the line in my persistence.xml
How can I manage to get both run in my application.
The problem is that the data source is managed by Wildfly which is not available on your test environment. So what you could do is define two separate persistence units (one for your production code and the other for the test) as follows:
<?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="primary">
<jta-data-source>java:/MySqlDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<!-- SQL stdout logging -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
</properties>
</persistence-unit>
<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
<class>org.se.bac.data.Employee</class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/empdb?useSSL=false"/>
<property name="hibernate.connection.username" value="student"/>
<property name="hibernate.connection.password" value="student"/>
<!-- SQL stdout logging -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
and then in your EmployeeDAOTest class modify the following line as:
private EntityManager em = JPA_HELPER.getEntityManager("testPU");
Note:
I removed the JDBC connection properties from the primary persistence unit because you don't need them as you already have the data source there on Wildfly.

#NamedStoredProcedureQuery NamedQuery of name: procuder not found

Greeting for the day..
Please provide me support.
I am new in Jpa and calling procuder with #NamedStoredProcedureQuery but my code is giving Exception as #NamedStoredProcedureQuery NamedQuery of name: procuder not found
my code is :=
#NamedStoredProcedureQuery(name = "TEST1",procedureName = "TEST1",parameters = { #StoredProcedureParameter(name = "P_CODE", mode = ParameterMode.IN, type = String.class), #StoredProcedureParameter(name = "P_DATE", mode = ParameterMode.IN, type = String.class), #StoredProcedureParameter(name = "P_CURSOR", mode = ParameterMode.REF_CURSOR, type = void.class), })
public class AnotherWayOfCallingProcedure implements Serializable
{
private static final String PERSISTENCE_UNIT_NAME = "todos";
private static EntityManagerFactory factory;
public static void main(String arg[]) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
StoredProcedureQuery addBookNamedStoredProcedure = em.createNamedStoredProcedureQuery("TEST1");
addBookNamedStoredProcedure.setParameter("P_CODE", "5000");
addBookNamedStoredProcedure.setParameter("P_DATE", "5/3/2017");
// Stored procedure call
List createdBookId = addBookNamedStoredProcedure.getResultList();
em.close();
}
}
and persistence.xml is
<?xml version="1.0" encoding="UTF-8" ?>
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
<class>com.app.IptAnchorageAreaMaster</class>
<class>com.app.AnotherWayOfCallingProcedure</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:#192.168.6.147:1521:orcl" />
<property name="javax.persistence.jdbc.user" value="aaa_kkk" />
<property name="javax.persistence.jdbc.password" value="aaa" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="none" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
</persistence>

How to configure Hibernate+JPA with Configuration?

I am new to JPA . Earlier I know Hibernate. But as per the requirement, I need to enhance to Hibernate+JPA Configuration.
Please Look my configuration.
POJO:
package com.skc.order;
import javax.annotation.Generated;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="blc_order")
public class Orders {
#Id
#Column(name="order_id")
Long orderId;
#Column(name="ORDER_NUMBER")
String orderNumber;
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
}
Persistance.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="ExpensePersistentUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>tamSql</non-jta-data-source>
<class>com.skc.order.Orders</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/broadleaf" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.connection.username" value="mysql" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
EntityManagerTest Class :
package com.skc.order;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class EntityManagerTest {
/*#PersistenceContext(unitName = "ExpensePersistentUnit")
protected EntityManager em;*/
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ExpensePersistentUnit");
EntityManager em = emf.createEntityManager();
Query qry = em.createNativeQuery("select * from blc_order", Orders.class);
Orders order = em.find(Orders.class, 1512);
System.out.println(order.getOrderNumber());
}
}
Jar Added In the ClassPath :
antlr-2.7.7
dom4j-1.6.1
hibernate-commons-annotations-4.0.1.Final
hibernate-core-4.2.0.Final
hibernate-jpa-2.0-api-1.0.1.Final
hibernate-validator-4.3.1.Final
javassist-3.15.0-GA
jboss-logging-3.1.2.GA
jboss-logmanager-1.4.0.Final
jboss-transaction-api_1.1_spec-1.0.1.Final
log4j-jboss-logmanager-1.0.1.Final
slf4j-api-1.6.1
slf4j-log4j12-1.6.1
commons-logging-1.1.1
hibernate-3.2.3.ga
hibernate-entitymanager-3.3.2.GA
And I am getting exception like :
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.hibernate.cfg.Configuration.<init>(Lorg/hibernate/cfg/SettingsFactory;)V from class org.hibernate.ejb.Ejb3Configuration
at org.hibernate.ejb.Ejb3Configuration.<init>(Ejb3Configuration.java:134)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:124)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at com.skc.order.EntityManagerTest.main(EntityManagerTest.java:13)
Please Help me out to solve this Problem.
Thanks ...
For Hibernate-core use 3.6.3 version..
This error used to come due to the class version mismatch. So I also got the same error just few minutes back. I just changed the version and that error is gone now. I am using hibernate core 3.6.3.FINAL with spring version 4.1.7.RELEASE. Hope this will work for you too. If you have doubt please comment.

JPA - Entities not persisted

Here 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="miniDS" transaction-type="JTA">
<jta-data-source>java:/miniDS</jta-data-source>
<class>com.company.model.Ordre</class>
<properties>
<!-- Options Hibernate -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.default_schema" value="mini" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
</properties>
</persistence-unit>
My code :
// Create order
Ordre o = new Ordre();
o.setDate(req.getParameter("date"));
o.setMotif(req.getParameter("motif"));
log.info("Ordre: " + o.getDate() + " " + o.getMotif());
OrdreService os = new OrdreService();
os.persist(o);//This method is NOT even called !
// Process application flow here...
OrdreService.java :
public class OrdreService {
private OrdreDAO dao;
public OrdreService() {
dao = new OrdreDAO();
}
public void persist(Ordre o) {
System.out.println("Service persist");
dao.persist(o);
}
//...
}
OrdreDAO.java :
public class OrdreDAO {
private EntityManagerFactory emf;
private EntityManager em;
public OrdreDAO() {
emf = Persistence.createEntityManagerFactory("miniDS");
em = emf.createEntityManager();
}
public void persist(Ordre o) {
System.out.println("DAO persist");
EntityTransaction et = null;
try {
et = em.getTransaction();
et.begin();
em.persist(o);
System.out.println("commit ?");
if (et != null) {
if (et.isActive()) {
et.commit();
}
}
} catch (Throwable t) {
t.printStackTrace();
if (et != null) {
if (et.isActive()) {
et.rollback();
}
}
}
}
//...
}
OrdreService.persist is never called :\ OrdreDAO.persist too.
What's going on with JBoss ?
JBoss 5.1.0.GA
Postgresql 8.3
JPA 1
When you use '<jta-data-source>' set your transaction type to JTA in persistence.xml file:
<persistence-unit name="your_pu_name" transaction-type="JTA">

Categories

Resources