Arquillian integration tests and OpenJPA enhancement - jpa

I'm working on a set of integration tests using arquillian and dbunit. I can run some of my tests just fine, but not the ones involving entities which have oneToMany relations with data in them. When running my tests I then get a PersistenceException:
Caused by: java.lang.NullPointerException
at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setInverseRelation(JDBCStoreManager.java:451)
My test looks like this:
#RunWith(Arquillian.class)
#CreateSchema("sql/masterplanCreateTables.sql")
public class MasterPlanManagerBeanDbIT {
#Rule
public PersistenceUnitRule rule = new PersistenceUnitRule();
#Inject
private MasterplanManager instance;
#PersistenceContext
EntityManager entityManager;
#Deployment
public static WebArchive createDeployment() throws Exception {
return ShrinkWrap
.create(WebArchive.class, .....
}
#Test
#UsingDataSet("/data/integration/uttrans/masterplan/validData_dbInput.xml")
public void updateTrip_givenValidInput_expectsTripToBeUpdated() {
Trip input = givenTrips().get(0);
input.setNote("updated value");
Trip updated = instance.updateTrip(input);
checkEquality(input, updated);//checks field by field for equality
}
}
My pom.xml looks like this:
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.1.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
...
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
...
</dependencies>
...
I did try the openjpa-maven-plugin to enhance the entities at build-time, but this will afaik alter the compiled entities which later will be deployed to our production environment (using deploy-time enhancement).
With that in mind, is it possible to enable deploy-time enhancement in my arquillian tests?

As I'm using openJpa, I found this link very helpful:
http://openejb.apache.org/javaagent.html
I am simply providing the openejb java-agent to the maven surefire plugin.

If you are using eclipse add -javaagent:{your java agent path} to the VM arguments for the test.
In my case I took the java agent directly from a TomEE installation.

Related

Connecting to a running Gemfire server

Using gfsh I have started a locator on my PC. How do I connect to this locator using spring-data-gemfire.
Here is the my locator
gfsh>start locator --name=locator1
Locator in /Users/adas/locator1 on 10.1.51.200[10334] as locator1 is currently online
Now I have the following class to connect to this locator
#ClientCacheApplication(locators = {
#ClientCacheApplication.Locator(host = "10.1.51.200", port = 10334)
})
#EnableEntityDefinedRegions(basePackageClasses = Person.class,
clientRegionShortcut = ClientRegionShortcut.LOCAL)
#EnableGemfireRepositories
public class GemfiretestApplication {
public static void main(String[] args) {
SpringApplication.run(GemfiretestApplication.class, args);
}
On compiling I get the following error
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gemfireCache': FactoryBean threw exception on object creation; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:178)
Given the INCREDIBLY limited information provided (partial stacktrace, no versions, no maven/gradle build files) I could only venture a guess at your problem. I think #juanramos has the correct idea.
I'm testing with:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Lovelave-SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
AND
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
Without the full stack trace I believe that I see your error. If then add the following exclusion, this problem disappears.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
If this does not solve your problem, please forward your complete stacktrace... or maybe the last "Caused by:...." statement and/or list of your dependencies.

Why does my flapdoodle Embedded MongoDB test fail to run? (creating 'embeddedMongoServer' could not start process EOF)

I'm having trouble getting my brand new project to build. I used https://start.spring.io/ to generate a fresh new Spring 2.0 MongoDB Maven project, and I want to have an embedded MongoDB database for my integration tests. The spring initializer added a dependency for de.flapdoodle.embed.mongo to that end.
But every time I try to run a "mvn clean package", I get the following error during my test:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'embeddedMongoServer' defined in class path resource
[org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]:
Invocation of init method failed; nested exception is java.io.IOException:
Could not start process: <EOF>
at de.flapdoodle.embed.mongo.AbstractMongoProcess.onAfterProcessStart(AbstractMongoProcess.java:79) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.process.runtime.AbstractProcess.<init>(AbstractProcess.java:116) ~[de.flapdoodle.embed.process-2.0.2.jar:na]
at de.flapdoodle.embed.mongo.AbstractMongoProcess.<init>(AbstractMongoProcess.java:53) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.mongo.MongodProcess.<init>(MongodProcess.java:50) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:44) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:34) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.process.runtime.Executable.start(Executable.java:108) ~[de.flapdoodle.embed.process-2.0.2.jar:na]
What am I missing?
My Application file is pretty straightforward:
#SpringBootApplication
public class NewnewinternetApplication {
public static void main(String[] args) {
SpringApplication.run(NewnewinternetApplication.class, args);
}
}
My Config file is very simple:
#Configuration
#EnableMongoRepositories
#ComponentScan(basePackages = "com.snoop.dougg.newnewinternet")
public class AppConfig {
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}
}
I have two simple controllers returning just static output for now.
I have a little document:
#Document(collection = "user")
public class User implements Serializable {
protected static final long serialVersionUID = -1L;
#Id
private String id;
private String username;
private String firstName;
private String lastName;
public User() {}
public User(String username, String firstName, String lastName) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
}
//Getters, setters, and equals and hash code methods...
}
And then a silly little test:
#RunWith(SpringRunner.class)
//#SpringBootTest -> Doesn't work either
#DataMongoTest
public class NewnewinternetApplicationTests {
#Autowired
private MongoTemplate mongoTemplate;
#Test
public void sillyLittleTest() {
mongoTemplate.save(new User("sdoug", "Snoop", "Dougg"));
Assert.notNull(
mongoTemplate.find(
new Query(Criteria.where("firstName").is("Snoop")), User.class),
"Couldn't find by first name!");
}
}
And then my pom file, which I really just left alone:
<?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.snoop.dougg.newnewinternet</groupId>
<artifactId>NewNewInternet</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>NewNewInternet</name>
<description>A new new internet</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<azure.version>2.0.1</azure.version>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-active-directory-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-keyvault-secrets-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-boot-bom</artifactId>
<version>${azure.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
I was in the same situation, and I could resolve it using #DirtiesContext on this way:
#DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class CommoditiesApplicationTests {
}
Commenting out the following lines in application.properties and placing them in a different profile can also work. I found it here
spring.data.mongodb.database=
spring.data.mongodb.host=
spring.data.mongodb.port=
Usually already running mongodb instance is the source of the issue. I would start with checking if anything occupies default mongodb port - 27017.
In my case the 32 bit mongodb client was downloaded instead of the 64 bit one.
embedded.mongo library uses BitSize class to determine the OS architecture. In my system System.getProperty("os.arch") was not returning a value listed in the if statement.
I solved the problem by setting os.arch system property to x86_64 (one of the values used by BitSize to return B64) in my application main.
#SpringBootApplication
public class Application {
public static void main(String[] args) {
System.setProperty("os.arch", "x86_64");
SpringApplication.run(Application.class, args);
}
}
Note: System.getProperty("os.arch") will return the wrong value if you use a 32 bit java version to run your application on a 64 bit system!
my error message was exactly like this
2022-03-15 10:57:00.053 WARN 7196 --- [ Test worker] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Unsatisfied dependency expressed through method 'embeddedMongoServer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoConfiguration' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.flapdoodle.embed.mongo.config.MongodConfig]: Factory method 'embeddedMongoConfiguration' threw exception; nested exception is java.lang.IllegalStateException: Set the spring.mongodb.embedded.version property or define your own MongodConfig bean to use embedded MongoDB
so, I add property in my application.yml file.
spring.mongodb.embedded.version: 3.2.3
and, solved it.
chances are the instance of mongodb downloaded through the spring plugin is 32 & you are running on 64 bit java or vice versa. Please confirm if there is any other way you have identified the fix.
I had the pretty same scenario here, and solved it using
<dependency>
<groupId>com.github.fakemongo</groupId>
<artifactId>fongo</artifactId>
<version>2.1.1</version>
<scope>test</scope>
</dependency>
instead of de.flapdoodle.embed.mongo
In my case, the socket file was still around.
To get to the underlying issue, I wanted the console logging output, I put a breakpoint in the else clause of the AbstractMongoProcess::onAfterProcessStart (which is hit on failure).
Here you have access to the logWatch and can run a System.out.println(logWatch.output.toString()); in debug mode to get the mongo console out. For my issue, the output said SocketException: Address already in use
Trying commands suggested such as sudo lsof -iTCP -sTCP:LISTEN -n -P did not work for me (nothing listed in my case)
I found another SO answer that said to run ls -lrta /tmp | grep .sock
The .sock file was still there from a previous run (Apparently I had interrupted my tests)
Deleting this file solved the issue.
The error is due to the package de.flapdoodle.embed which was used for Embedded Mongo, use a stable version of it 3.5.0
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>3.5.0</version>
<scope>test</scope>
</dependency>
add it in the pom.file(add the version to it).Then update maven it should work fine
My case was a bit special, but maybe this help someone else too to resolve this.
If, by any chance, you are using win 10 and you have already a MongoDB running as a service (in my case it was an earlier version - v3.4 - running), then try to stop the service, and run the test afterwards.
Try to add #DirtiesContext to the test class level.
I deleted the 'mongo' dir in my appdata/temp and that is when I caught my McAfee quarantining my embedded mongo. I turned OFF McAfee and deleted the temp mongo again and then all ran great...
Same problem.
spring.data.mongodb.port was 27017 in application.properties. I changed it to 0. When 0 is used, a random port is assigned instead.
My Integration Test is like below:
#RunWith(SpringRunner.class)
#DataMongoTest
public class IntegrationTestIT { ... }
I'm using de.flapdoodle.embed:de.flapdoodle.embed.mongo:3.4.6.
I had the same issue. Refer to this github issue for the solution if your problem is related to flapdoodle: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/427
Just increase the flapdoodle version >=3.5.0
and if possible increase also spring-boot version to the latest

cannot use eclipselink with spring boot

I have a Spring Boot 1.4 app, which I'm trying to use Eclipselink instead Hibernate.
For now, this is what I did:
Removed Hibernate from deps
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
Added eclipselink
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.3</version>
</dependency>
Configured JpaBaseConfiguration
#Configuration
public class JpaConfiguration extends JpaBaseConfiguration {
protected JpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
super(dataSource, properties, jtaTransactionManagerProvider);
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(final EntityManagerFactoryBuilder builder, #Autowired DataSource dataSource) {
final LocalContainerEntityManagerFactoryBean ret = builder.dataSource(dataSource)
.packages("com.inkdrop.app.domain.models")
.persistenceUnit("chathub-perstence-unit")
.properties(getVendorProperties())
.build();
return ret;
}
#Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}
#Override
protected Map<String, Object> getVendorProperties() {
return Collections.singletonMap("eclipselink.weaving", "false");
}
when I run the app, it creates the tables in database (representing my models), and then, throws this error:
Caused by: java.lang.ClassCastException: org.eclipse.persistence.internal.jpa.metamodel.SingularAttributeImpl cannot be cast to javax.persistence.metamodel.PluralAttribute
at org.eclipse.persistence.internal.jpa.querydef.FromImpl.join(FromImpl.java:485)
at org.springframework.data.jpa.repository.query.QueryUtils.getOrCreateJoin(QueryUtils.java:596)
at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:524)
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:332)
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:275)
at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:180)
at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:109)
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:118)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:241)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:68)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214)
I tried following this and this tutorials, but didn't helped
I think that your problem is an incompatibility between Spring Data version and the EclipseLink version that you use.
Eclipselink support in SpringBoot seems bad.
In the pom.xml of spring-boot-dependencies :
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.RELEASE</version>
in properties tags, you can see :
<properties>
...
<hibernate.version>5.0.9.Final</hibernate.version>
<hibernate-validator.version>5.2.4.Final</hibernate-validator.version>
...
</properties>
but zero information about Eclipselink.
In the same way, in the spring-boot-starter-data-jpa pom, you have a direct dependency for hibernate-core in dependencies tag :
<dependencies>
...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
...
<dependencies>
but no dependency for EclipseLink in dependenciesManagement tag. It's a pity since it could have given a way, for projets to use on demand a EclipseLink version compatible with current Spring Boot version in this way :
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
</dependency>
But as this feature is missing, you must specify the version of Eclipslink in your pom.xml :
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.3</version>
</dependency>
In your case, it may be complicated to guess which version of Eclipselink is compatible with the 1.4 spring boot version since your tutorial use the 1.3.2 spring boot version.
To address your problem, I advise you to try with the same versions (EclipseLink and Spring Boot) as in your tutorial or any working application using SpringBoot and EclipseLink to check that it's well a version compatibility problem.
If it's ok, then and if you want to update your Spring Boot or EclipseLink version or both, you must do tests to validate a winning combination between Spring Boot, and EclipseLink versions.

Java-ee REST server with IntelliJ and Tomcat

I'm trying to implement a REST Server API using Java-ee following this tutorial. Instead of Glassfish, I use Tomcat.
I could develop a servlet
#WebServlet(name = "hello", urlPatterns = "/")
public class HelloWorld extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("=)");
}
}
And join http://localhost:9080/ I can see the smiling face. But when I try to access to the api path (http://localhost:9080/api/recommend/all) I also get the face. If I remove the servlet class, I get a 404 error. I suppose I need something else to build automatically the api but I don't know what.
Could someone tell my what is missing? What should I do?
Update:
In Intellij's Java Enterprise View I see:
Web > HelloWorld
RESTful WS > recommend > all
These are my api classes:
#ApplicationPath("/api")
public class REST_Config extends Application {
}
And the specific method
#Path("recommend")
public class RecommenderController {
#Path("/all")
#GET
#Produces("application/json")
public JsonArray getAll(){
JsonArrayBuilder builder = Json.createArrayBuilder();
builder.add(Json.createObjectBuilder().add("1", "2.5"));
return builder.build();
}
}
And the 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>TestREST</groupId>
<artifactId>TestREST</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
</dependencies>
<build>
<finalName>TestREST</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
</project>
"Instead of Glassfish, I use Tomcat."
Look at this
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
This is nothing more than basically a bunch of interfaces for the EE spec. There is no implementation. Java EE servers will have the implementation. Tomcat is not an EE server. The only part of the EE spec it will definitely implements is the Servlet Specification. You are trying to work with the JAX-RS spec, where Tomcat for sure by default does not have an implementation for. So you need to add that implementation.
The easiest IMO to get started with, is Jersey. You can simple add this dependency
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.17</version>
</dependency>
And it will get you up and running. Keep the Jersey User Guide handy. It will come in use.
Also I don't know what JsonArray is, but what will happen when you run this is you will get some error similar to "No MessageBodyWriter found for JsonArray and media type application/json". You need to provider. If you are going to use the Java EE JSONP API, then you should add the this provider
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>2.17</version>
</dependency>
As you get to working alot with JSON, you wil find this API to be difficult to maintain. I'd recommend using Jackson. If you don't already know it, I'd suggest learning it. It offers simple POJO to JSON mapping. For Jackson, you can add this dependency
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>

java hibernate 4 org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.MYSQLDialect

I'm learning Java EE and Hibernate, and I've run into the following problem:
I've created a Dynamic Web Project in Eclipse, I've converted it into a MAVEN project, added the mysql-connector-java and the hibernate-core dependencies to the pom.xml.
I've added a servlet, and in the doGet method, I've tried to initialize the sessionfactory to create the tables. I'm using tomcat.
When I'm making a request to the servlet I'm getting the following exception:
org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.MYSQLDialect
org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:77)
org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:65)
org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:146)
org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)
hu.adamsan.testhibernate.TestHibernate.doGet(TestHibernate.java:74)
pom.xml:
dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.6.Final</version>
</dependency>
</dependencies>
I've tried to look into the Maven Dependencies, org.hibernate.dialect.MySQLDialect.class is in there, in the location: getServletContext().getRealPath(".") ->E:\eclips_jee\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\TestHibernateWeb\
, in the WEB-INF\lib directory, there is hibernate-core-4.2.6.Final.jar, when I opened it with 7zip, I could find inside the org/hibernate/dialect/MySQL5Dialect.class.
What am I missing? I really have no idea.
doGet method:
PrintWriter w = response.getWriter();
w.println("Testing hibernate<br/>");
w.println(getServletContext().getRealPath(".")+"<br/>");
Configuration config;
ServiceRegistry registry;
SessionFactory factory;
config = new Configuration().addAnnotatedClass(Modell.class);
Properties props = getProperties();
props.setProperty(Environment.HBM2DDL_AUTO, "create");
config.setProperties(props);
registry = new ServiceRegistryBuilder().applySettings(props).buildServiceRegistry();
factory = config.buildSessionFactory(registry);
factory.close();
The answer is in the question. You've found MySQLDialect and MySQL5Dialect in your jar files, but your configuration file tries to use MYSQLDialect. Java is case-sensitive.
I'm puting this because the above answer wasn't accurate for me, In my case I'm using Intellij IDEA While working with Hibernate dependency I was over configuring the pom dependency with a type (pom type) My solution is to remove that type.
<!-- Hibernate JPA dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.3.Final</version>
<type>pom</type>
</dependency>
The working version Is :
<!-- Hibernate JPA dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.3.Final</version>
</dependency>
NO POM TYPE
Which allowed to make ride of the following bug :
java.lang.TypeNotPresentException: Type org.hibernate.SessionFactory not present
that generally was blocking the resolve of the following property
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
and thus while doing the setup of hibernate session factory under the spring framework