What has changed between Hibernate 5.1.1 and 5.2.2? If I use 5.2.2 I'll get an error message "No Persistence provider for EntityManager named pu". Exactly the same configuration works with 5.1.1. How should I change my code to get 5.2.2 to work?
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jpatest</groupId>
<artifactId>jpatest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<hibernate.version>5.2.2.Final</hibernate.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1209.jre7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
</dependencies>
</project>
persistence.xml in src/main/resources/META-INF
<?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="pu" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="hibernate.default_schema" value="myschema" />
<property name="hibernate.connection.username" value="xxx" />
<property name="hibernate.connection.password" value="zzz" />
<!-- <property name="hibernate.show_sql" value="true"/> -->
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
</persistence>
creating EntityManager
factory = Persistence.createEntityManagerFactory("pu");
em = factory.createEntityManager();
tx = em.getTransaction();
The class org.hibernate.ejb.HibernatePersistence does not exist in the hibernate-release-5.2.2.Final.zip bundle file. That's why the provider can't be found, because the class can't (at the project library jars). Instead, I used the class org.hibernate.jpa.HibernatePersistenceProvider, which CAN be found at hibernate-core-5.2.2.Final.jar (that comes with hibernate-release-5.2.2.Final.zip bundle), by changing the provider at persistence.xml to <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>. Doing so, it worked fine! Hope the problem is only this.
I had the same error.
I changed version of
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
from 5.3.1.Final to 5.3.6.Final and error disappeared.
Sometimes simply forgetting to add the persistence.xml file to build path causes this problem. Follow the following steps:
Right click on persistence.xml file.
Click on Build Path
Add to build path
Then it should work.
Related
I'm trying to use the PersistenceContext annotation to inject an entity manager but I get the following exception.
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'Foo' available
Then I did some research and in every example there's a bean in the configuration class with the same information that I have in my persistence.xml.
Are we supposed to be able to inject the entitymanager with only the persistence-unit name?
Here's my code
#Component
public class UnitOfWork {
#PersistenceContext(unitName="Foo")
private EntityManager entityManager;
}
#Configuration
#ComponentScan("com.foo.package")
public class Config {
}
Persistence.xml in META-INF folder
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="Foo">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect"/>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/foo?useSSL=false" />
<property name="hibernate.connection.username" value="foo"/>
<property name="hibernate.connection.password" value="foo" />
</properties>
</persistence-unit>
</persistence>
Pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org-springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org-springframework.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
You still need to define a LocalContainerEntityManagerFactory to have the EntityManagerFactory available in the application context.
Without one that wouldn't work, the LocalContainerEntityManagerFactory will use the persistence.xml (when present) to configure itself.
I am tryig to set up JPA to my RESTful webapi in order to make CRUD services on database. I am getting the error Could not find any META-INF/persistence.xml file in the classpath But actually ther in a persistence.xml in the folder META-INF
1 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.6-Final
17 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.6-Final
20 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
23 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
27 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
110 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
115 [main] INFO org.hibernate.ejb.Version - Hibernate EntityManager 3.5.6-Final
129 [main] INFO org.hibernate.ejb.Ejb3Configuration - Could not find any META-INF/persistence.xml file in the classpath
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named KAPAPLAN
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at de.ham.ti.kapaplan.database.DBCon.main(DBCon.java:20)
here my persistence.xml
<persistence 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"
version="2.1">
<persistence-unit name="KAPAPLAN" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>de.ham.ti.kapaplan.model</class>
<properties>
<!-- Configuring JDBC properties -->
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:#//***db-domain***/***service-name***" />
<property name="javax.persistence.jdbc.user" value="***user***" />
<property name="javax.persistence.jdbc.password" value="***pw***" />
<property name="javax.persistence.jdbc.driver" value="com.mysema.query.jpa.support.ExtendedOracleDialect" />
<!-- Hibernate properties -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<!-- Configuring Connection Pool -->
<!-- <property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="2000" />-->
</properties>
</persistence-unit>
</persistence>
I double checked similar questions but non of them solved my issue. ann ideas?
EDIT: my Maven pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.ham.ti.kapaplan</groupId>
<artifactId>kapaplan</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>kapaplan</name>
<build>
<finalName>kapaplan</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I think your project is misconfigured. META-INF folder should stay in src/main/resources and WEB-INF folder in src/main/webapp of your web maven project. Here is an example where I do some CRUD over web api.
You need to move persistence.xml to a location on your app server's class path. For a Maven project, that is typically in the src/main/resources folder. Check the Hibernate documentation to see if Hibernate expects the file to be in a sub-folder, or if putting it in the root of the classpath is OK.
The easiest way to set the path for your META-INF in a maven project is to use the pom.xml and set the path directly over there by using the directory tag. I was facing the same problem but solved it using this.
ex:- your META-INF is present in the src folder
your pom.xml will be:
<dependencies>
..
</dependencies>
<build>
<resources>
<resource>
<directory>E:/Netbeans Repo/Hibernate/src</directory>
</resource>
</resources>
</build>
This should work
NOTE : This may sound duplicate but I have tried all possible methods
I am working on eclipse link with jpa (learning JPA) and came to know that JPA is not supporting EnitityManager#getCriteriaBuilder(). I am getting this error
The method getCriteriaBuilder() is undefined for the type EntityManager
Following is my dependencies
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.4</version>
</dependency>
But when I checked documentation, doesn't says about the deprecation of method.Any help will be useful
And I am using all this on J2SE project
Thanks
following is my persistence.xml
<persistence-unit name="simple-insert_fetch">
<class>com.model.Employee</class>
<properties>
<!-- Configuring JDBC properties -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_studies" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<!-- <property name="javax.persistence.schema-generation.database.action" -->
<!-- value="drop-and-create" /> -->
</properties>
</persistence-unit>
Following is the code I am using to fetch Result
Crite
riaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
Root<Employee> employee = query.from(Employee.class);
I'm not exactly sure how to phrase my question, but I have been working through a hibernate tutorial and everything has been smooth sailing until we switched from using the hibernate.cfg.xml to a resources/META-INF/persistance.xml. It seems that the program cannot create an EntityManagerFactory object, and is throwing an XsdException. After trying to research what that exception is, im not exactly sure what it means, or how to fix it. at this point just starting a session and closing the transaction would be a big step. what am i doing wrong? am i not putting the persistance.xml file in the right spot? any directional advice would be greatly appreciated...
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/internal/util/xml/XsdException
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:80)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at ApplicationJPA.main(ApplicationJPA.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.hibernate.internal.util.xml.XsdException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 11 more
src/main/java/applicationJPA.java (driver)
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class ApplicationJPA {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("infinite-finances");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.close();
emf.close();
}
}
src/main/recourses/META-INF/persistance.xml
<persistence 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" version="2.1">
<persistence-unit name="infinite-finances" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="passwprd"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/infinite_skills"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<!-- Create/update tables automatically using mapping metadata -->
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.infiniteSkills</groupId>
<artifactId>ifinance-hibernateTut</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.0.CR2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
</dependencies>
</project>
Not sure but could be the hibernate-entitymanager dependency, switch to this one:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
I have been working on a project, which is a simple Spring, Hibernate, JSF, MySQL integration; and I run it on Eclipse. The aim of the program is to add a Person(id, first name, last name, gender, age etc.) database on MySQLWorkbench and run it on server. I used Spring4, Hibernate4, Primefaces 5.0.2 and Eclipse Luna as tools.And I constructed the project as maven.
When I run the program on main, I want to see that a new Person is included in my table.
I used "GRANT ALL ON" and "FLUSH PRIVILEGES" commands for the user I have created, on terminal and received "Query OK" message.
Despite specifying username and password on necessary files, I cannot add a new record to my table.My Project structure is as follows:
And other files are:
domain-classes.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class table="testPerson" name="com.hibernate.data.Person">
<id column="person_id" type="int" name="id">
<generator class="increment" />
</id>
<property name="firstName" column="person_firstname" type="string" />
<property name="lastName" column="person_lastname" type="string" />
<property name="gender" column="person_gender" type="string" />
<property name="age" column="person_age" type="int" />
</class>
</hibernate-mapping>
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/PERSONDB</property>
<property name="hibernate.connection.username">fulden2</property>
<property name="hibernate.connection.password">secret_pass</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Show SQL -->
<property name="show_sql">true</property>
<!-- Specify session context -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Autocommit -->
<property name="hibernate.connection.autocommit">false</property>
<!-- Referring Mapping File -->
<mapping resource="domain-classes.hbm.xml" />
</session-factory>
</hibernate-configuration>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Enable Spring Annotation Configuration -->
<context:annotation-config />
<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.*"></context:component-scan>
<!-- Create Data Source bean -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/PERSONDB" />
<property name="username" value="fulden2" />
<property name="password" value="secret_pass" />
</bean>
<!-- Define SessionFactory bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Detect #Transactional Annotation -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>PersonApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>PersonApp</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
</build>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- Faces Implementation -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.10</version>
</dependency>
<!-- Faces Library -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.10</version>
</dependency>
<!-- Primefaces Version 5 -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.2</version>
</dependency>
<!-- JSP Library -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- JSTL Library -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Primefaces Theme Library -->
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>blitzer</artifactId>
<version>1.0.10</version>
</dependency>
<!-- Hibernate library -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.9.Final</version>
</dependency>
<!-- MySQL driver connector library -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>
</project>
Main class:
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.hibernate.data.Person;
public class Main {
public static void main(String [] args){
Configuration configuration = new Configuration();
System.out.println("CFG and hbm files loaded successfully.");//just to test
configuration.configure("hibernate.cfg.xml");
SessionFactory factory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().configure().build());
Session session = factory.openSession();
Transaction tx = session.getTransaction();
tx.begin();
System.out.println("Transaction began");//just to test
Person newPerson = new Person();
newPerson.setFirstName("aa");
newPerson.setLastName("bbb");
newPerson.setGender("Male");
newPerson.setAge(2);
session.save(newPerson);
session.flush();
tx.commit();
session.close();
System.out.println("Session closed");//just to test
}
}
Is there an extra step to achieve this database connection?
Or what is my mistake here?
Thanks for any help..
Probably your session is not getting flushed; might be a glitch in your configuration. It happens many a time for an instance if you have configured OpenSessionInView Filter it changes the default flush mode of your configuration. In the DAO layer check the flushMode session.getFlushMode()
The flush mode should not be NEVER or MANUAL
If the session is not getting flushed, you db will not be updated. You have to manually flush the session after your transaction by session.flush()
Also in the in configuration file just change the property to true from false & check <property name="connection.autocommit">true</property>