Transactional wrapped method dont persist or remove - java

Hi i got a problem with a CDI Named Bean wich is #Transactional.
I use the WildFly 11.1.0.Final.
Here the Code: https://gist.github.com/stefanwendelmann/7a1f8352900067d5a59826d6ee205044
This is the persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="QuoLoco" transaction-type="JTA">
<description>Verbindung zur QuoLoco Datenbank</description>
<jta-data-source>java:/QUOLOCO_NORM</jta-data-source>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.id.new_generator_mappings" value="false"/>
</properties>
</persistence-unit>
</persistence>
When i call the method "doChange" and only edited Empfangseinheittypenparameter, the em.remove and em.persist dont trigger a flush.
When i call the method "doChange" and edited some of the main  Empfangseinheittypen empfangseinheittyp, i make a find on the Empfangseinheittypenparameter dont need a flush
What am a doing wrong?
Both methods are public.
Please see a picture of the Application attatched
Thanks in advance for your help

According to the comments, doChange is called from the same bean.
The problem is similar to #Transactional method called from another method doesn't obtain a transaction which means, that the transaction will not end with doChange, so you can not expect a flush to be done when returning from the method.

Related

Cannot resolve column name

I'm using JPA with hibernate under my small resteasy project deployed on wildfly.
I've got my persistence.xml file under directory:
resources/META-INF/
<?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="PostgresDS" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
So I'm dropping and creating database each time on startup.
When typing annotation #Column(name="SomeName") I've got error that
"Cannot resolve column "Name" "
and as fix I'm supposed to attach data-source.
Should I prepare some hibernate config file(attaching data-source didn't help), maybe disabling some validation ?

Re-create database at runtime using EclipseLink

Is there a way to recreate the database dynamically at Run-Time in EclipseLink?
Right now I have it that if the database does not exist, it creates the database at compilation time :
<?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="DefaultUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.logging.level.sql" value="INFO" />
<property name="javax.persistence.schema-generation.database.action"
value="create" />
</properties>
</persistence-unit>
</persistence>
However, I want to be able to Drop, and Recreate the database while the application is running. I.E when a user passes in some special flag, I want to call something that will drop the current database, and regenerate it.
I found that you can do that in Hibernate using the SchemaExport class
You can add <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> into persistence.xml.
It will delete all table and create it again. Be careful, all your earlier data will be lost.
absolutely. Properties can be added dynamically at runtime, so you can add the "eclipselink.ddl-generation" with value "drop-and-create-tables" when you create the EntityManagerFactory for the first time to have it drop and create the database. Of course, this isn't much use to a running app where you want to change things on the fly.
To get around this, EclipseLInk has a few tricks that allow dynamic changes and reloading of the persistence unit. Try
Map properties = new HashMap();
properties.put("eclipselink.ddl-generation", "drop-and-create-tables");
properties.put("eclipselink.ddl-generation.output-mode", "database");
//this causes DDL generation to occur on refreshMetadata rather than wait until an em is obtained
properties.put("eclipselink.deploy-on-startup", "true");
JpaHelper.getEntityManagerFactory(em).refreshMetadata(properties);
The org.eclipse.persistence.jpa.JPAHelper class is just used to unwrap the Factory to get a JpaEntityManagerFactory instance to make the non-JPA refreshMetadata call. Any number of properties or settings can be changed, and any new EntityManagers obtained after the refresh call will reflect those changes.

JBoss EAP 6.3 CMT Multiple persistence units

This is essentially a duplicate of How to locate the source of JBAS011470 error in JBoss?
But essentially, As soon as I add a second persistence unit, it gives me this error. It's ridiculous. I'm not going to disable the JPA subsystem like some people suggest - that sounds wrong.
My persistence.xml setup is as follows, where java:/NAME is set up as a datasource in standalone.xml:
<?xml version="1.0"?>
<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="name" transaction-type="JTA">
<jta-data-source>java:/NAME</jta-data-source>
<class>za.co.classes.A</class>
<class>za.co.classes.B</class>
<class>za.co.classes.C</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="jboss.entity.manager.factory.jndi.name"
value="java:jboss/persistence/NAME" />
<property name="jboss.entity.manager.jndi.name"
value="java:jboss/persistence/em/NAME" />
<property name="hibernate.dialect" value="za.co.equrahealth.dao.SQLServerDialect" />
</properties>
</persistence-unit>
The error starts as soon as I add a second persistence unit. So spring context is irrelevant.
Well, I luckily have multiple databases within the same schema, so I came up with a workaround. But it's obviously not going to solve the problem when there are multiple schemas. I think the cause of this issue might actually be a bug in JBoss.
#PersistenceContext
private EntityManager entityManager;
private EntityManager getEntityManager(String source)
{
if ("a".equalsIgnoreCase(source))
{
entityManager.createNativeQuery("USE A_DB;").executeUpdate();
}
else
{
entityManager.createNativeQuery("USE B_DB").executeUpdate();
}
return entityManager;
}

Ljava.lang.String class cannot be resolved in persistence.xml

I am using JBoss Developer Studio. I have a project with a persistence.xml file. The file is perfect to me, but I keep getting this error on the tab that lists all the Problems of my project.
Class "[Ljava.lang.String;#22ec7158" cannot be resolved
I include the picture for a better context.
When I click on the error, so that it takes me to the place where the error is happening, it takes me to the end of the file.
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MemberOfferDS</jta-data-source>
<class>com.bbb.memberoffer.model.SycsCoordinator</class>
<class>com.bbb.aicweb.memberoffer.model.SycsCoordinatorPhoneNumber</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorClub</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorSecurityGroup</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorClubPk</class>
<class>com.bbb.memberoffer.model.PhoneNumberType</class>
<class>com.bbb.memberoffer.model.Club</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Not sure if this is the right one to use or not? -->
<property name='hibernate.show_sql' value='true' />
<property name='hibernate.format_sql' value='true' />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
</properties>
</persistence-unit>
</persistence>
You may have solved this already, however, in my case the problem was caused by referencing a class in a element that no longer existed.
EDIT: The exact reason for this type of error message is that the error generator is trying to run a toString() on a String array when it generates the error message.

Glassfish 4 - TransactionRequiredException using Hibernate and CDI #Transactional

I am getting the following exception:
javax.el.ELException: javax.persistence.TransactionRequiredException
at com.sun.el.parser.AstValue.invoke(AstValue.java:279)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
Caused by: javax.persistence.TransactionRequiredException
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:161)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:299)
I am using Hibernate 4.3.5
#Named
#SessionScoped
public class MenuBean implements Serializable {
#PersistenceContext
private EntityManager entityManager;
#Transactional
public void create() {
MenuTitle menu = new MenuTitle();
menu.setLabel(label);
entityManager.persist(menu); //exception in this line
label = null;
}
Persistence XML:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="MyPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>MySQL5</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.current_session_context_class" value="jta"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</properties>
</persistence-unit>
I have also tried to set hibernate.transaction.jta.platform to org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform but it results in the same error.
EJB CMT´s are working fine.
The Create action is called from a commandButton:
<p:commandButton value="Create" process="#this type" update=":megaMenuForm:mainMenu" actionListener="#{menuBean.create()}" oncomplete="closeMenuDialog(xhr, status, args)"/>
See EJB specs if you want to manage transaction. Inject a resource UserTransaction.
Also #Transactional with not do anything here since you are using EJB.
In general you example should work, and #Transactional annotation will be just ignored. Could you try to make an interface, and call create method through it?
Looks like you are using EJB 2 style coding, but Glassfish 4 implements EJB 3.1, so it is better to go with a new one.

Categories

Resources