I'm praticing with JPA API. I got an error
java.lang.ClassNotFoundException: javax.persistence.Persistence cannot be found
My code below:
EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("mail");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("SELECT v FROM Version v");
List<Version> versions = query.getResultList();
The error at line emf = Persistence.createEntityManagerFactory("mail");
Any solution?
You are trying to set up a standalone JPA project. In order to do so you need a JPA provider jars. The two more popular providers are Eclipselink and Hibernate. If you are using maven you can add dependencies to their implementations.
For Eclipselink
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
For Hibernate
<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>4.3.6.Final</version>
</dependency>
If you are not using maven you can download their implementations from their sites and put it in your classpath.
For Eclipselink: http://www.eclipse.org/eclipselink/downloads/
For Hibernate: http://hibernate.org/orm/
Some JPA quickstarts are recommending to add only the JPA API (interface declarations only) dependencies with maven.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
or
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
This approach will be successful only in server environment as the server will provide appropriate implementation at runtime.
Right Click on Project -> Properties -> Search "Deployment Assembly" -> Add Maven Dependencies.
if using maven , add below dependency in Pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
then clean install maven
Related
I am trying to integrate Spring 4 with Hibernate 4 and i am using eclipse ide with out maven dependencies,But i am getting below mentioned error:
Caused by: java.lang.classnotfoundexception : org.hibernate.annotations.Entity
if any one has implemented already and working fine, So please can you share the project for reference so that i can understand the flow of integration, As i am new to spring and hibernate.I need some good guidance.
you need to use some dependency, which you can find below :
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.5-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.9.Final</version>
</dependency>
and if you are not using maven project then you need to download the hibernate-entitymanager and hibernate-annotations jar and add into build path and rebuild the project
I have a Spring MVC project using Maven.
I am using Spring-data-jpa as one of my dependency:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.5.RELEASE</version>
</dependency>
Spring-data-jpa provides the api.
Therefore, I need to add another dependency which implements jpa. However, I am confused about:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.6.4</version>
</dependency>
and
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.2.Final</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
What is eclipse.persistence? And the difference with hibernate?
Please help!
Eclipse Persistence (EclipseLink) and Hibernate are both implementations of Java Persistence API, each with their own extra features and often their own bugs. In terms of speed, they're very similar to each-other, compared to the other implementations.
Only one of is used in each persistence unit, defined in META-INF/persistence.xml. Look at yours, inside the persistence-unit->provider node you will find the class that is used.
If it starts with org.hibernate, then you can safely remove the eclipse dependency.
If it starts with org.eclipse, you can remove the hibernate dependency.
If you have multiple persistence units, each one can use a different implementation/provider.
I'm building a library where I scan a class and check if their field as OneToMany and ManyToOne annotations. I currently added eclipselink 3.6 as dependecy of my module, like this
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.0</version>
</dependency>
but I don't want to make my lib dependent on eclipselink, I want it able to be used with any JPA implementation. How do i do that?
Unfortunately, there's no standard package that provides only the annotations/interface (like, for example, on the servlet spec). Each ORM has their own package, but they all follow the jpa standard. What you can do is declare the dependency as optional.
For Eclipselink
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
<optional>true</optional>
</dependency>
Probably you'll need to depend on eclipselink for your tests, so you can mark the original dependency only for test...
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
I use in my project h2 and I like to enable the lucene search index. I added the following deps. to the pom.xml:
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>4.4.0</version>
</dependency>
I still get java.lang.ClassNotFoundException: org.apache.lucene.search.Searcher from h2. This problem only occours when I launch the program.
Use lucene < 4.0.0; the org.apache.lucene.search.Searcher abstract class has been deprecated in version 3.6.0, and then included since 4.0.0 as/inside org.apache.lucene.search.IndexSearcher.
http://lucene.apache.org/core/3_6_0/api/core/deprecated-list.html
I have a setup with JPA (hibernate + postgresql) and MongoDB:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
My Repository Interface is:
#Repository
public interface JpaModelRepository extends JpaRepository<ModelEntity, Integer> {
public ModelEntity findByName(String modelNameSample);
}
My Entity:
#Entity
#Table (name = "model")
public class ModelEntity implements GenericId<Integer>, Serializable {
....
#Column (name = "name_tx")
private String name;
Implementation of the repository interface is auto-generated by SpringData. If I remove the findByName, all works fine in my project. If I leave it I have this in tomcat error log:
Caused by: java.lang.NoSuchMethodError: org.springframework.data.repository.query.parser.Part.getProperty()Lorg/springframework/data/repository/query/parser/Property;
at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:163)
at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:95)
at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:49)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:102)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:59)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:164)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:71)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:269)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:142)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:114)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:38)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 85 more
Tried to use this documentation but failed for some reason:
Spring Doc
Hope is one of you guys managed to create a custom query (like findBySomeProperty(...)) ...
Thank you
The Spring data APIs changed bewteen 1.0 and 1.1 releases, that is why this error. Use the same version for both of your dependencies and it should work.
I had the same problem of
org.springframework.data.repository.query.parser.PartTree.isCountProjection()Ljava/lang/Boolean;
But I resolved it by using a proper combination of jars like
spring-data-jpa-1.2.0.RELEASE.jar <BR>
spring-data-commons-1.6.3.RELEASE.jar <BR>
spring-data-commons-core-1.4.1.RELEASE.jar<BR>
hibernate-entitymanager-4.1.12.final.jar<BR>
hibernate-jpa-2.1-api-1.0.0.final.jar<BR>
hibernate-validator-4.2.0.final.jar<BR>
hibernate-commons-annotations-4.0.0.CR2.jar<BR>
and using Spring core 3.2.5 version of jars. The problem was solved.
I was also using a proper dialect, i.e. the right driver to connect to the database.
I found the answer:
<spring.version>3.1.2.RELEASE</spring.version>
<spring-data.jpa.version>1.2.0.BUILD-SNAPSHOT</spring-data.jpa.version>
<spring-data.mongodb.version>1.1.0.BUILD-SNAPSHOT</spring-data.mongodb.version>
https://github.com/SpringSource/spring-data-multistore-test
I did some maven test combining version on that git repository
I got the same problem when trying to use Spring Data JPA + Hibernate + Spring Framework 4.0. The problem is that the Spring Data project doesn't keep-up at the same rate as the Spring Framework project.
I resolve the problem using the proper JAR version combination and excluding any other Spring project, so that those depended JAR could be downloaded correctly and automatically based on the "Spring Data" pom.xml file. Check here all the Spring Data dependencies: http://search.maven.org/#artifactdetails%7Corg.springframework.data%7Cspring-data-jpa%7C1.6.0.RELEASE%7Cjar
I changed my maven pom.xml file to:
<properties>
<!-- Generic properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>3.2.9.RELEASE</spring-framework.version>
<spring-data.version>1.6.0.RELEASE</spring-data.version>
<!-- Hibernate / JPA -->
<hibernate.version>3.6.10.Final</hibernate.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data.version}</version>
</dependency>
<!-- Hibernate -->
<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>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>${hibernate-jpa.version}</version>
</dependency>
....
</dependencies>