I want to use QueryDSL with JDBC (Not JPA). So in pom.xml I put these dependencies and plugin:
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>3.7.4</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>3.7.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
....
<plugins>
<plugin>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>3.7.4</version>
<executions>
<execution>
<goals>
<goal>export</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbc.url>jdbc:mysql://localhost:8889/chebuoni</jdbc.url>
<jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
<jdbcUser>root</jdbcUser>
<jdbcPassword>root</jdbcPassword>
<packageName>it.group.myproject</packageName>
<targetFolder>/target/generated-sources/java</targetFolder>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
Maven can do its building without errors only if I out generic-export as goal, but in this case it does not create the Q class I need in order to use QueryDSL. I also have my class Person mapped with annotation @Entity in package it.group.myproject
********************** edit ********************** Following @Nikolas tips I was finally able to generate Q class, and I add in the pom this plugin:
<plugin>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>4.1.0</version>
<configuration>
<jdbc.url>jdbc:mysql://localhost:8889/myDB</jdbc.url>
<jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
<jdbc.user>root</jdbc.user>
<jdbc.password>root</jdbc.password>
<packageName>myPackage</packageName>
<targetFolder>/target/generated-sources/java</targetFolder>
<namePrefix>S</namePrefix>
<imports>
<import>java.io</import>
<import>java.io.File</import>
</imports>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
</dependency>
</dependencies>
</plugin>
But still, there is the part of the entityManager that I don't know how to configure:
JPAQuery<?> queryFactory = new JPAQuery<Void>(entityManager);
I read this tutorial --> https://examples.javacodegeeks.com/enterprise-java/jpa/jpa-entitymanager-example/ in which it seems I need a persistence.xml file so that I can do this:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jcg-JPA");
EntityManager em = emf.createEntityManager();
so I configured persistence and the entitymanager bean in this way:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jcg-JPA" />
</bean>
And this is persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="jcg-JPA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:8889/myDB" />
</properties>
</persistence-unit>
But server fails at startup:
org.apache.catalina.core.StandardContext listenerStart
GRAVE: Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
java.lang.NoSuchMethodError:
org.springframework.beans.factory.annotation.InjectionMetadata: method
<init>()V not found at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:351)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:295)
You seem to confuse some things here, JDBC and JPA are two different pair of shoes, refer to this post. You can not go without JPA here as it aids with the creation of the metamodel. You even refer to it yourself by using @Entity. You will use both JDBC and JPA.
That being said, your library is quite out of date. They restructured their package structure some time ago. I guess that this results in the NPE, as the maven plugin expects com.querydsl classes.
This is the current Git project. Check the maven config on the .md, this one should work.
This is the documentation to refer to.
(Edit)
EntityManager via persistence.xml
I know that "I don't want no Hibernate"-Feeling, at this point you might find a proper JPA-Implementation useful, though. You could implement JPA yourself, but ... don't do it, yet. Anyway, the EntityManager is JPA-Territory. If you don't like Hibernate, use EclipseLink. But chose one. Then, follow the tutorials on how to set up your persistence.xml (in which you specify the entity manager factory, introduce the persistent classes and configure the DB-Connection). Then you can refer to the factory by name as your tutorial mentioned. Configuring JPA is out of the scope of the QueryDSL-Tut, just chose a how-to the JPA-"Implementor" provides, they are quite decent.
Spring
Check this post about setting up JPA the "Spring-Way", maybe that is what you wanted. It still requires you to make a choice in regards to Hibernate, though. That is why I kinda left the paragraph above.
(Edit2) Regarding your persistence.xml, I think you might need to name all your persistent classes (Another link, another Tutorial) in your xml, at the moment you specified none, so the scanner does not find any. In Spring, you can specify "packages to scan", which basically does a similar job.