mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
worked on EclipseLink support
This commit is contained in:
parent
e2c9f24812
commit
aee53413dc
@ -71,7 +71,7 @@
|
||||
<dependency>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
<artifactId>eclipselink</artifactId>
|
||||
<version>2.1.0-M7</version>
|
||||
<version>2.1.0-SNAPSHOT</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
@ -81,7 +81,14 @@
|
||||
<version>${hsqldb.version}</version>
|
||||
<scope>test</scope>
|
||||
<!-- license : TODO -->
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.2.133</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derby</artifactId>
|
||||
|
||||
@ -281,6 +281,7 @@ public class HQLSerializer extends SerializerBase<HQLSerializer> {
|
||||
}
|
||||
|
||||
private void visitCast(Expr<?> source, Class<?> targetType) {
|
||||
// NOT : this is not supported in JPQL, only HQL
|
||||
append("cast(").handle(source);
|
||||
append(AS);
|
||||
append(targetType.getSimpleName().toLowerCase(Locale.ENGLISH)).append(")");
|
||||
|
||||
@ -65,7 +65,8 @@ public class HQLTemplates extends Templates {
|
||||
add(Ops.ARRAY_SIZE, "size({0})");
|
||||
|
||||
// string
|
||||
add(Ops.CONCAT, "concat({0},{1})", 0);
|
||||
add(Ops.LIKE, "{0} like {1}",1);
|
||||
add(Ops.CONCAT, "concat({0},{1})",0);
|
||||
add(Ops.MATCHES, "{0} like {1}", 27); // TODO : support real regexes
|
||||
add(Ops.LOWER, "lower({0})");
|
||||
add(Ops.SUBSTR_1ARG, "substring({0},{1}+1)");
|
||||
@ -82,8 +83,8 @@ public class HQLTemplates extends Templates {
|
||||
add(Ops.ENDS_WITH_IC, "{0l} like {%%1}");
|
||||
add(Ops.STARTS_WITH, "{0} like {1%}");
|
||||
add(Ops.STARTS_WITH_IC, "{0l} like {1%%}");
|
||||
add(Ops.INDEX_OF, "locate({1},{0})-1");
|
||||
add(Ops.INDEX_OF_2ARGS, "locate({1},{0},{2}+1)-1");
|
||||
add(Ops.INDEX_OF, "locate({1},{0}) - 1");
|
||||
add(Ops.INDEX_OF_2ARGS, "locate({1},{0},{2}+1) - 1");
|
||||
|
||||
// date time
|
||||
add(Ops.DateTimeOps.SYSDATE, "sysdate");
|
||||
|
||||
@ -54,6 +54,32 @@ public abstract class AbstractJPATest extends AbstractStandardTest{
|
||||
assertFalse(results.isEmpty());
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testQuery(){
|
||||
// String queryStr = "select cat.name, otherCat.name " +
|
||||
// "from Cat cat, Cat otherCat " +
|
||||
// "where length(cat.name) > :a1 and length(otherCat.name) > :a1 and locate(otherCat.name,cat.name) - 1 > :a1";
|
||||
//
|
||||
// javax.persistence.Query query = entityManager.createQuery(queryStr);
|
||||
// query.setParameter("a1", 1);
|
||||
// query.getResultList();
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// public void testQuery2(){
|
||||
// String queryStr = "select cat.name, otherCat.name " +
|
||||
// "from Cat cat, Cat otherCat " +
|
||||
// "where length(cat.name) > :a1 and length(otherCat.name) > :a1 and " +
|
||||
// "cat.name like concat(:a2,concat(substring(otherCat.name,:a3+1,:a4),:a2))";
|
||||
//
|
||||
// javax.persistence.Query query = entityManager.createQuery(queryStr);
|
||||
// query.setParameter("a1", 0);
|
||||
// query.setParameter("a2", "XXX");
|
||||
// query.setParameter("a3",0);
|
||||
// query.setParameter("a4",0);
|
||||
// query.getResultList();
|
||||
// }
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void tupleProjection(){
|
||||
|
||||
@ -31,14 +31,18 @@ public class EclipseLinkTemplates extends HQLTemplates{
|
||||
add(Ops.STARTS_WITH, "locate({1},{0})=1");
|
||||
add(Ops.STARTS_WITH_IC, "locate({1l},{0l})=1");
|
||||
|
||||
// EclipseLink specific
|
||||
// EclipseLink specific (works at least with Derby, HSQLDB and H2)
|
||||
add(Ops.DateTimeOps.SECOND, "func('second',{0})");
|
||||
add(Ops.DateTimeOps.MINUTE, "func('minute',{0})");
|
||||
add(Ops.DateTimeOps.HOUR, "func('hour',{0})");
|
||||
add(Ops.DateTimeOps.DAY_OF_WEEK, "func('dayofweek',{0})");
|
||||
add(Ops.DateTimeOps.DAY_OF_MONTH, "func('day',{0})");
|
||||
add(Ops.DateTimeOps.DAY_OF_YEAR, "func('dayofyear',{0})");
|
||||
add(Ops.DateTimeOps.MONTH, "func('month',{0})");
|
||||
add(Ops.DateTimeOps.WEEK, "func('week',{0})");
|
||||
add(Ops.DateTimeOps.YEAR, "func('year',{0})");
|
||||
add(Ops.DateTimeOps.YEAR_MONTH, "func('year',{0}) * 100 + func('month',{0})");
|
||||
|
||||
}
|
||||
|
||||
public boolean wrapElements(Operator<?> operator){
|
||||
@ -50,6 +54,7 @@ public class EclipseLinkTemplates extends HQLTemplates{
|
||||
}
|
||||
|
||||
public boolean isTypeAsString() {
|
||||
// TODO : get rid of this when Hibernate supports type(alias)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query._h2;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.mysema.query.AbstractJPATest;
|
||||
import com.mysema.query.EclipseLinkTemplates;
|
||||
import com.mysema.query.Target;
|
||||
import com.mysema.query.hql.HQLTemplates;
|
||||
import com.mysema.testutil.JPAConfig;
|
||||
import com.mysema.testutil.JPATestRunner;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
@RunWith(JPATestRunner.class)
|
||||
@JPAConfig("h2-eclipselink")
|
||||
public abstract class H2JPAEclipseLinkTest extends AbstractJPATest{
|
||||
|
||||
protected HQLTemplates getTemplates(){
|
||||
return EclipseLinkTemplates.DEFAULT;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Target getTarget() {
|
||||
return Target.H2;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query._hsqldb;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.mysema.query.AbstractJPATest;
|
||||
import com.mysema.query.EclipseLinkTemplates;
|
||||
import com.mysema.query.Target;
|
||||
import com.mysema.query.hql.HQLTemplates;
|
||||
import com.mysema.testutil.JPAConfig;
|
||||
import com.mysema.testutil.JPATestRunner;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
@RunWith(JPATestRunner.class)
|
||||
@JPAConfig("hsqldb-eclipselink")
|
||||
public abstract class HSQLDBJPAEclipseLinkStandardTest extends AbstractJPATest{
|
||||
|
||||
protected HQLTemplates getTemplates(){
|
||||
return EclipseLinkTemplates.DEFAULT;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Target getTarget() {
|
||||
return Target.HSQLDB;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -40,13 +40,39 @@
|
||||
<property name="hibernate.dialect" value="com.mysema.query.hql.support.ExtendedHSQLDialect"/>
|
||||
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
|
||||
<property name="hibernate.connection.url" value="jdbc:hsqldb:file:target/testdb"/>
|
||||
<property name="hibernate.connection.username" value="sa"/>
|
||||
<property name="hibernate.connection.user" value="sa"/>
|
||||
<property name="hibernate.show_sql" value="true"/>
|
||||
<property name="hibernate.flushMode" value="FLUSH_AUTO"/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="hsqldb-eclipselink" transaction-type="RESOURCE_LOCAL">
|
||||
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
|
||||
<exclude-unlisted-classes>false</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/testdb2"/>
|
||||
<property name="javax.persistence.jdbc.user" value="sa"/>
|
||||
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
|
||||
<property name="eclipselink.ddl-generation.output-mode" value="database" />
|
||||
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="h2-eclipselink" transaction-type="RESOURCE_LOCAL">
|
||||
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
|
||||
<exclude-unlisted-classes>false</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:target/h2"/>
|
||||
<property name="javax.persistence.jdbc.user" value="sa"/>
|
||||
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
|
||||
<property name="eclipselink.ddl-generation.output-mode" value="database" />
|
||||
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="mysql">
|
||||
<properties>
|
||||
<property name="hibernate.archive.autodetection" value="class" />
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
|
||||
hibernate.connection.driver_class=com.mysql.jdbc.Driver
|
||||
hibernate.connection.url=jdbc:mysql://localhost:3306/querydsl
|
||||
hibernate.connection.username=root
|
||||
hibernate.connection.password=
|
||||
hibernate.connection.username=querydsl
|
||||
hibernate.connection.password=querydsl
|
||||
|
||||
## Common properties
|
||||
hibernate.show_sql=true
|
||||
|
||||
@ -52,8 +52,7 @@ public class SQLInsertClause implements InsertClause<SQLInsertClause> {
|
||||
|
||||
private final List<Expr<?>> values = new ArrayList<Expr<?>>();
|
||||
|
||||
public SQLInsertClause(Connection connection, SQLTemplates templates,
|
||||
PEntity<?> entity) {
|
||||
public SQLInsertClause(Connection connection, SQLTemplates templates, PEntity<?> entity) {
|
||||
this.connection = connection;
|
||||
this.templates = templates;
|
||||
this.entity = entity;
|
||||
|
||||
@ -17,6 +17,10 @@ import com.mysema.query.sql.SQLQuery;
|
||||
import com.mysema.query.sql.SQLQueryImpl;
|
||||
import com.mysema.query.sql.SQLSubQuery;
|
||||
import com.mysema.query.sql.SQLTemplates;
|
||||
import com.mysema.query.sql.dml.SQLDeleteClause;
|
||||
import com.mysema.query.sql.dml.SQLInsertClause;
|
||||
import com.mysema.query.sql.dml.SQLUpdateClause;
|
||||
import com.mysema.query.types.path.PEntity;
|
||||
|
||||
public abstract class AbstractBaseTest {
|
||||
|
||||
@ -29,7 +33,19 @@ public abstract class AbstractBaseTest {
|
||||
|
||||
@Nullable
|
||||
protected String expectedQuery;
|
||||
|
||||
|
||||
protected SQLUpdateClause update(PEntity<?> e){
|
||||
return new SQLUpdateClause(Connections.getConnection(), dialect, e);
|
||||
}
|
||||
|
||||
protected SQLInsertClause insert(PEntity<?> e){
|
||||
return new SQLInsertClause(Connections.getConnection(), dialect, e);
|
||||
}
|
||||
|
||||
protected SQLDeleteClause delete(PEntity<?> e){
|
||||
return new SQLDeleteClause(Connections.getConnection(), dialect, e);
|
||||
}
|
||||
|
||||
protected SQLQuery query() {
|
||||
return new SQLQueryImpl(Connections.getConnection(), dialect) {
|
||||
@Override
|
||||
|
||||
@ -85,7 +85,7 @@ public final class Connections {
|
||||
private static Connection getMySQL() throws SQLException, ClassNotFoundException {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
String url = "jdbc:mysql://localhost:3306/querydsl";
|
||||
return DriverManager.getConnection(url, "root", "");
|
||||
return DriverManager.getConnection(url, "querydsl", "querydsl");
|
||||
}
|
||||
|
||||
private static Connection getOracle() throws SQLException, ClassNotFoundException{
|
||||
|
||||
@ -5,7 +5,6 @@
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import static com.mysema.query.Connections.getConnection;
|
||||
import static com.mysema.query.Constants.survey;
|
||||
import static com.mysema.query.Constants.survey2;
|
||||
|
||||
@ -16,25 +15,15 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.sql.SQLSubQuery;
|
||||
import com.mysema.query.sql.dml.SQLDeleteClause;
|
||||
import com.mysema.query.sql.dml.SQLInsertClause;
|
||||
import com.mysema.query.sql.domain.QEmployee;
|
||||
import com.mysema.query.sql.domain.QSurvey;
|
||||
import com.mysema.query.types.path.PEntity;
|
||||
|
||||
public abstract class InsertBaseTest extends AbstractBaseTest{
|
||||
|
||||
protected SQLInsertClause insert(PEntity<?> e){
|
||||
return new SQLInsertClause(getConnection(), dialect, e);
|
||||
}
|
||||
|
||||
protected SQLDeleteClause delete(PEntity<?> e){
|
||||
return new SQLDeleteClause(Connections.getConnection(), dialect, e);
|
||||
}
|
||||
|
||||
private void reset() throws SQLException{
|
||||
delete(survey).where(survey.name.isNotNull()).execute();
|
||||
Connections.getStatement().execute("insert into survey values (1, 'Hello World')");
|
||||
insert(survey).values(1, "Hello World").execute();
|
||||
}
|
||||
|
||||
@Before
|
||||
|
||||
@ -9,7 +9,6 @@ import static com.mysema.query.Constants.survey;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@ -17,24 +16,13 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.sql.dml.SQLDeleteClause;
|
||||
import com.mysema.query.sql.dml.SQLUpdateClause;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.path.PEntity;
|
||||
|
||||
public abstract class UpdateBaseTest extends AbstractBaseTest{
|
||||
|
||||
protected SQLUpdateClause update(PEntity<?> e){
|
||||
return new SQLUpdateClause(Connections.getConnection(), dialect, e);
|
||||
}
|
||||
|
||||
protected SQLDeleteClause delete(PEntity<?> e){
|
||||
return new SQLDeleteClause(Connections.getConnection(), dialect, e);
|
||||
}
|
||||
|
||||
private void reset() throws SQLException{
|
||||
protected void reset() throws SQLException{
|
||||
delete(survey).where(survey.name.isNotNull()).execute();
|
||||
Connections.getStatement().execute("insert into survey values (1, 'Hello World')");
|
||||
insert(survey).values(1, "Hello World").execute();
|
||||
}
|
||||
|
||||
@Before
|
||||
|
||||
@ -45,8 +45,9 @@ public class FilteringTestRunner extends JUnit4ClassRunner {
|
||||
|
||||
@Override
|
||||
public void run(final RunNotifier notifier) {
|
||||
if (run)
|
||||
if (run){
|
||||
super.run(notifier);
|
||||
}
|
||||
}
|
||||
|
||||
protected TestMethod wrapMethod(Method method) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user