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);
Related
Im trying to solve it an Hibernate problem from yesterday, I already check my libraries and classes and i think im not missing anything, so Im not sure about the real problem on this, the error tha Im getting is:
Exception in thread "main" java.lang.NoSuchMethodError: 'void org.hibernate.cfg.AnnotationBinder.bindDefaults(org.hibernate.boot.spi.MetadataBuildingContext)'
Error message
I already uploaded all the correct dependecies/libraries, I hope can be other kind of issue, libraries already are there
POM imge file
thanks a lot for help me on this, already found the solution, let me share to you the solution in below code format:
<dependencies>
<!-- Hibernate framework 5.6.10.Final-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
this is the correct dependecies structure, now the hibernate configuration is the following:
<property name = "hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property>
<!-- Assume test is the database name -->
<property name = "hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property>
<property name = "hibernate.connection.username">root</property>
<property name = "hibernate.connection.password">mysqlroot</property>
<!-- JDBC Connection pool -->
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Every session will run in separate thread and thread safe -->
<property name="current_session_context_class">thread</property>
<!-- Disable second level cache -->
<property name = "hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed sql to stdout -->
<property name="show_sql">true</property>
<!-- drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- names the annotated entity class -->
<mapping class="com.to.Player"/>
hope can i help you with this, good day!
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.
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.
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>
I'm using Hibernate in a Maven project. The DBMS is MySQL. The libraries are imported into the pom as follows:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
Now, when I try in Netbeans to run a simple HQL query (i.e. from User), I get the following error:
org.hibernate.HibernateException: could not instantiate QueryTranslatorFactory: org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
at org.hibernate.cfg.SettingsFactory.createQueryTranslatorFactory(SettingsFactory.java:436)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:258)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
Caused by: java.lang.ClassCastException: org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory cannot be cast to org.hibernate.hql.QueryTranslatorFactory
at org.hibernate.cfg.SettingsFactory.createQueryTranslatorFactory(SettingsFactory.java:433)
... 7 more
The strangest thing is that I see a ClassCastException for org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory. I see from here that the correct configuration into the hibernate.cfg.xml for the hibernate.query.factory_class is:
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
Moreover, my hibernate.cfg.xml configuration file is:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/datadb?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="edu/project/entities/User.hbm.xml"/>
<mapping resource="edu/project/entities/Code.hbm.xml"/>
<mapping resource="edu/project/entities/Incident.hbm.xml"/>
</session-factory>
</hibernate-configuration>
What's wrong?
PS: the MySQL server is online. Moreover I tried other queries, like select u from User.