use of org.hibernate.dialect.HSQLDialect in project hibernate-orm by hibernate.
the class MapBinder method createFormulatedValue.
protected Value createFormulatedValue(Value value, Collection collection, String targetPropertyName, PersistentClass associatedClass, PersistentClass targetPropertyPersistentClass, MetadataBuildingContext buildingContext) {
Value element = collection.getElement();
String fromAndWhere = null;
if (!(element instanceof OneToMany)) {
String referencedPropertyName = null;
if (element instanceof ToOne) {
referencedPropertyName = ((ToOne) element).getReferencedPropertyName();
} else if (element instanceof DependantValue) {
//TODO this never happen I think
if (propertyName != null) {
referencedPropertyName = collection.getReferencedPropertyName();
} else {
throw new AnnotationException("SecondaryTable JoinColumn cannot reference a non primary key");
}
}
Iterator<Selectable> referencedEntityColumns;
if (referencedPropertyName == null) {
referencedEntityColumns = associatedClass.getIdentifier().getColumnIterator();
} else {
Property referencedProperty = associatedClass.getRecursiveProperty(referencedPropertyName);
referencedEntityColumns = referencedProperty.getColumnIterator();
}
fromAndWhere = getFromAndWhereFormula(associatedClass.getTable().getName(), element.getColumnIterator(), referencedEntityColumns);
} else {
// HHH-11005 - only if we are OneToMany and location of map key property is at a different level, need to add a select
if (!associatedClass.equals(targetPropertyPersistentClass)) {
fromAndWhere = getFromAndWhereFormula(targetPropertyPersistentClass.getTable().getQualifiedTableName().toString(), element.getColumnIterator(), associatedClass.getIdentifier().getColumnIterator());
}
}
if (value instanceof Component) {
Component component = (Component) value;
Iterator properties = component.getPropertyIterator();
Component indexComponent = new Component(getBuildingContext().getMetadataCollector(), collection);
indexComponent.setComponentClassName(component.getComponentClassName());
while (properties.hasNext()) {
Property current = (Property) properties.next();
Property newProperty = new Property();
newProperty.setCascade(current.getCascade());
newProperty.setValueGenerationStrategy(current.getValueGenerationStrategy());
newProperty.setInsertable(false);
newProperty.setUpdateable(false);
newProperty.setMetaAttributes(current.getMetaAttributes());
newProperty.setName(current.getName());
newProperty.setNaturalIdentifier(false);
//newProperty.setOptimisticLocked( false );
newProperty.setOptional(false);
newProperty.setPersistentClass(current.getPersistentClass());
newProperty.setPropertyAccessorName(current.getPropertyAccessorName());
newProperty.setSelectable(current.isSelectable());
newProperty.setValue(createFormulatedValue(current.getValue(), collection, targetPropertyName, associatedClass, associatedClass, buildingContext));
indexComponent.addProperty(newProperty);
}
return indexComponent;
} else if (value instanceof SimpleValue) {
SimpleValue sourceValue = (SimpleValue) value;
SimpleValue targetValue;
if (value instanceof ManyToOne) {
ManyToOne sourceManyToOne = (ManyToOne) sourceValue;
ManyToOne targetManyToOne = new ManyToOne(getBuildingContext().getMetadataCollector(), collection.getCollectionTable());
targetManyToOne.setFetchMode(FetchMode.DEFAULT);
targetManyToOne.setLazy(true);
//targetValue.setIgnoreNotFound( ); does not make sense for a map key
targetManyToOne.setReferencedEntityName(sourceManyToOne.getReferencedEntityName());
targetValue = targetManyToOne;
} else {
targetValue = new SimpleValue(getBuildingContext().getMetadataCollector(), collection.getCollectionTable());
targetValue.copyTypeFrom(sourceValue);
}
Iterator columns = sourceValue.getColumnIterator();
Random random = new Random();
while (columns.hasNext()) {
Object current = columns.next();
Formula formula = new Formula();
String formulaString;
if (current instanceof Column) {
formulaString = ((Column) current).getQuotedName();
} else if (current instanceof Formula) {
formulaString = ((Formula) current).getFormula();
} else {
throw new AssertionFailure("Unknown element in column iterator: " + current.getClass());
}
if (fromAndWhere != null) {
formulaString = Template.renderWhereStringTemplate(formulaString, "$alias$", new HSQLDialect());
formulaString = "(select " + formulaString + fromAndWhere + ")";
formulaString = StringHelper.replace(formulaString, "$alias$", "a" + random.nextInt(16));
}
formula.setFormula(formulaString);
targetValue.addFormula(formula);
}
return targetValue;
} else {
throw new AssertionFailure("Unknown type encounters for map key: " + value.getClass());
}
}
use of org.hibernate.dialect.HSQLDialect in project hibernate-orm by hibernate.
the class StandardDialectResolver method resolveDialect.
@Override
public Dialect resolveDialect(DialectResolutionInfo info) {
final String databaseName = info.getDatabaseName();
if ("CUBRID".equalsIgnoreCase(databaseName)) {
return new CUBRIDDialect();
}
if ("HSQL Database Engine".equals(databaseName)) {
return new HSQLDialect();
}
if ("H2".equals(databaseName)) {
return new H2Dialect();
}
if ("MySQL".equals(databaseName)) {
final int majorVersion = info.getDatabaseMajorVersion();
if (majorVersion >= 5) {
return new MySQL5Dialect();
}
return new MySQLDialect();
}
if ("PostgreSQL".equals(databaseName)) {
final int majorVersion = info.getDatabaseMajorVersion();
final int minorVersion = info.getDatabaseMinorVersion();
if (majorVersion == 9) {
if (minorVersion >= 4) {
return new PostgreSQL94Dialect();
} else if (minorVersion >= 2) {
return new PostgreSQL92Dialect();
}
return new PostgreSQL9Dialect();
}
if (majorVersion == 8 && minorVersion >= 2) {
return new PostgreSQL82Dialect();
}
return new PostgreSQL81Dialect();
}
if ("EnterpriseDB".equals(databaseName)) {
return new PostgresPlusDialect();
}
if ("Apache Derby".equals(databaseName)) {
final int majorVersion = info.getDatabaseMajorVersion();
final int minorVersion = info.getDatabaseMinorVersion();
if (majorVersion > 10 || (majorVersion == 10 && minorVersion >= 7)) {
return new DerbyTenSevenDialect();
} else if (majorVersion == 10 && minorVersion == 6) {
return new DerbyTenSixDialect();
} else if (majorVersion == 10 && minorVersion == 5) {
return new DerbyTenFiveDialect();
} else {
return new DerbyDialect();
}
}
if ("ingres".equalsIgnoreCase(databaseName)) {
final int majorVersion = info.getDatabaseMajorVersion();
final int minorVersion = info.getDatabaseMinorVersion();
switch(majorVersion) {
case 9:
if (minorVersion > 2) {
return new Ingres9Dialect();
}
return new IngresDialect();
case 10:
return new Ingres10Dialect();
default:
LOG.unknownIngresVersion(majorVersion);
}
return new IngresDialect();
}
if (databaseName.startsWith("Microsoft SQL Server")) {
final int majorVersion = info.getDatabaseMajorVersion();
switch(majorVersion) {
case 8:
{
return new SQLServerDialect();
}
case 9:
{
return new SQLServer2005Dialect();
}
case 10:
{
return new SQLServer2008Dialect();
}
case 11:
case 12:
case 13:
{
return new SQLServer2012Dialect();
}
default:
{
if (majorVersion < 8) {
LOG.unknownSqlServerVersion(majorVersion, SQLServerDialect.class);
return new SQLServerDialect();
} else {
// assume `majorVersion > 13`
LOG.unknownSqlServerVersion(majorVersion, SQLServer2012Dialect.class);
return new SQLServer2012Dialect();
}
}
}
}
if ("Sybase SQL Server".equals(databaseName) || "Adaptive Server Enterprise".equals(databaseName)) {
return new SybaseASE15Dialect();
}
if (databaseName.startsWith("Adaptive Server Anywhere")) {
return new SybaseAnywhereDialect();
}
if ("Informix Dynamic Server".equals(databaseName)) {
return new InformixDialect();
}
if ("DB2 UDB for AS/400".equals(databaseName)) {
return new DB2400Dialect();
}
if (databaseName.startsWith("DB2/")) {
return new DB2Dialect();
}
if ("Oracle".equals(databaseName)) {
final int majorVersion = info.getDatabaseMajorVersion();
switch(majorVersion) {
case 12:
return new Oracle12cDialect();
case 11:
// fall through
case 10:
return new Oracle10gDialect();
case 9:
return new Oracle9iDialect();
case 8:
return new Oracle8iDialect();
default:
LOG.unknownOracleVersion(majorVersion);
}
return new Oracle8iDialect();
}
if ("HDB".equals(databaseName)) {
// SAP recommends defaulting to column store.
return new HANAColumnStoreDialect();
}
if (databaseName.startsWith("Firebird")) {
return new FirebirdDialect();
}
return null;
}
use of org.hibernate.dialect.HSQLDialect in project hibernate-orm by hibernate.
the class ASTParserLoadingTest method testExpressionWithParamInFunction.
@Test
public void testExpressionWithParamInFunction() {
Session s = openSession();
s.beginTransaction();
s.createQuery("from Animal a where abs(a.bodyWeight-:param) < 2.0").setLong("param", 1).list();
s.createQuery("from Animal a where abs(:param - a.bodyWeight) < 2.0").setLong("param", 1).list();
if ((getDialect() instanceof HSQLDialect) || (getDialect() instanceof DB2Dialect)) {
// HSQLDB and DB2 don't like the abs(? - ?) syntax. bit work if at least one parameter is typed...
s.createQuery("from Animal where abs(cast(:x as long) - :y) < 2.0").setLong("x", 1).setLong("y", 1).list();
s.createQuery("from Animal where abs(:x - cast(:y as long)) < 2.0").setLong("x", 1).setLong("y", 1).list();
s.createQuery("from Animal where abs(cast(:x as long) - cast(:y as long)) < 2.0").setLong("x", 1).setLong("y", 1).list();
} else {
s.createQuery("from Animal where abs(:x - :y) < 2.0").setLong("x", 1).setLong("y", 1).list();
}
if (getDialect() instanceof DB2Dialect) {
s.createQuery("from Animal where lower(upper(cast(:foo as string))) like 'f%'").setString("foo", "foo").list();
} else {
s.createQuery("from Animal where lower(upper(:foo)) like 'f%'").setString("foo", "foo").list();
}
s.createQuery("from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0").setLong("param", 1).list();
if (getDialect() instanceof DB2Dialect) {
s.createQuery("from Animal where lower(upper('foo') || upper(cast(:bar as string))) like 'f%'").setString("bar", "xyz").list();
} else {
s.createQuery("from Animal where lower(upper('foo') || upper(:bar)) like 'f%'").setString("bar", "xyz").list();
}
if (getDialect() instanceof AbstractHANADialect) {
s.createQuery("from Animal where abs(cast(1 as double) - cast(:param as double)) = 1.0").setLong("param", 1).list();
} else if (!(getDialect() instanceof PostgreSQLDialect || getDialect() instanceof PostgreSQL81Dialect || getDialect() instanceof MySQLDialect)) {
s.createQuery("from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0").setLong("param", 1).list();
}
s.getTransaction().commit();
s.close();
}
use of org.hibernate.dialect.HSQLDialect in project hibernate-orm by hibernate.
the class ASTParserLoadingTest method testComponentNullnessChecks.
@Test
public void testComponentNullnessChecks() {
Session s = openSession();
s.beginTransaction();
Human h = new Human();
h.setName(new Name("Johnny", 'B', "Goode"));
s.save(h);
h = new Human();
h.setName(new Name("Steve", null, "Ebersole"));
s.save(h);
h = new Human();
h.setName(new Name("Bono", null, null));
s.save(h);
h = new Human();
h.setName(new Name(null, null, null));
s.save(h);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
List results = s.createQuery("from Human where name is null").list();
assertEquals(1, results.size());
results = s.createQuery("from Human where name is not null").list();
assertEquals(3, results.size());
String query = (getDialect() instanceof DB2Dialect || getDialect() instanceof HSQLDialect) ? "from Human where cast(? as string) is null" : "from Human where ? is null";
if (getDialect() instanceof DerbyDialect) {
s.createQuery(query).setParameter(0, "null").list();
} else {
s.createQuery(query).setParameter(0, null).list();
}
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery("delete Human").executeUpdate();
s.getTransaction().commit();
s.close();
}
use of org.hibernate.dialect.HSQLDialect in project hibernate-orm by hibernate.
the class FooBarTest method testPersistCollections.
@Test
public void testPersistCollections() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
assertEquals(0l, s.createQuery("select count(*) from Bar").iterate().next());
assertEquals(0l, s.createQuery("select count(*) from Bar b").iterate().next());
assertFalse(s.createQuery("from Glarch g").iterate().hasNext());
Baz baz = new Baz();
s.save(baz);
baz.setDefaults();
baz.setStringArray(new String[] { "stuff" });
Set bars = new HashSet();
bars.add(new Bar());
baz.setCascadingBars(bars);
HashMap sgm = new HashMap();
sgm.put("a", new Glarch());
sgm.put("b", new Glarch());
baz.setStringGlarchMap(sgm);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
assertEquals(1L, ((Long) s.createQuery("select count(*) from Bar").iterate().next()).longValue());
baz = (Baz) ((Object[]) s.createQuery("select baz, baz from Baz baz").list().get(0))[1];
assertTrue(baz.getCascadingBars().size() == 1);
//System.out.println( s.print(baz) );
Foo foo = new Foo();
s.save(foo);
Foo foo2 = new Foo();
s.save(foo2);
baz.setFooArray(new Foo[] { foo, foo, null, foo2 });
baz.getFooSet().add(foo);
baz.getCustoms().add(new String[] { "new", "custom" });
baz.setStringArray(null);
baz.getStringList().set(0, "new value");
baz.setStringSet(new TreeSet());
Time time = new java.sql.Time(12345);
baz.getTimeArray()[2] = time;
//System.out.println(time);
assertTrue(baz.getStringGlarchMap().size() == 1);
//The following test is disabled databases with no subselects
if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof PointbaseDialect)) {
List list = s.createQuery("select foo from Foo foo, Baz baz where foo in elements(baz.fooArray) and 3 = some elements(baz.intArray) and 4 > all indices(baz.intArray)").list();
assertTrue("collection.elements find", list.size() == 2);
}
if (!(getDialect() instanceof SAPDBDialect)) {
// SAPDB doesn't like distinct with binary type
List list = s.createQuery("select distinct foo from Baz baz join baz.fooArray foo").list();
assertTrue("collection.elements find", list.size() == 2);
}
List list = s.createQuery("select foo from Baz baz join baz.fooSet foo").list();
assertTrue("association.elements find", list.size() == 1);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
assertEquals(1, ((Long) s.createQuery("select count(*) from Bar").iterate().next()).longValue());
baz = (Baz) s.createQuery("select baz from Baz baz order by baz").list().get(0);
assertTrue("collection of custom types - added element", baz.getCustoms().size() == 4 && baz.getCustoms().get(0) != null);
assertTrue("component of component in collection", baz.getComponents()[1].getSubcomponent() != null);
assertTrue(baz.getComponents()[1].getBaz() == baz);
assertTrue("set of objects", ((FooProxy) baz.getFooSet().iterator().next()).getKey().equals(foo.getKey()));
assertTrue("collection removed", baz.getStringArray().length == 0);
assertTrue("changed element", baz.getStringList().get(0).equals("new value"));
assertTrue("replaced set", baz.getStringSet().size() == 0);
assertTrue("array element change", baz.getTimeArray()[2] != null);
assertTrue(baz.getCascadingBars().size() == 1);
//System.out.println( s.print(baz) );
baz.getStringSet().add("two");
baz.getStringSet().add("one");
baz.getBag().add("three");
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
baz = (Baz) s.createQuery("select baz from Baz baz order by baz").list().get(0);
assertTrue(baz.getStringSet().size() == 2);
assertTrue(baz.getStringSet().first().equals("one"));
assertTrue(baz.getStringSet().last().equals("two"));
assertTrue(baz.getBag().size() == 5);
baz.getStringSet().remove("two");
baz.getBag().remove("duplicate");
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
assertEquals(1, ((Long) s.createQuery("select count(*) from Bar").iterate().next()).longValue());
baz = (Baz) s.load(Baz.class, baz.getCode());
assertTrue(baz.getCascadingBars().size() == 1);
Bar bar = new Bar();
Bar bar2 = new Bar();
s.save(bar);
s.save(bar2);
baz.setTopFoos(new HashSet());
baz.getTopFoos().add(bar);
baz.getTopFoos().add(bar2);
assertTrue(baz.getCascadingBars().size() == 1);
baz.setTopGlarchez(new TreeMap());
GlarchProxy g = new Glarch();
s.save(g);
baz.getTopGlarchez().put('G', g);
HashMap map = new HashMap();
map.put(bar, g);
map.put(bar2, g);
baz.setFooToGlarch(map);
map = new HashMap();
map.put(new FooComponent("name", 123, null, null), bar);
map.put(new FooComponent("nameName", 12, null, null), bar);
baz.setFooComponentToFoo(map);
map = new HashMap();
map.put(bar, g);
baz.setGlarchToFoo(map);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
baz = (Baz) s.createQuery("select baz from Baz baz order by baz").list().get(0);
assertTrue(baz.getCascadingBars().size() == 1);
Session s2 = openSession();
Transaction txn2 = s2.beginTransaction();
assertEquals(3, ((Long) s2.createQuery("select count(*) from Bar").iterate().next()).longValue());
Baz baz2 = (Baz) s2.createQuery("select baz from Baz baz order by baz").list().get(0);
Object o = baz2.getFooComponentToFoo().get(new FooComponent("name", 123, null, null));
assertTrue(o == baz2.getFooComponentToFoo().get(new FooComponent("nameName", 12, null, null)) && o != null);
txn2.commit();
s2.close();
assertTrue(Hibernate.isInitialized(baz.getFooToGlarch()));
assertTrue(baz.getTopFoos().size() == 2);
assertTrue(baz.getTopGlarchez().size() == 1);
assertTrue(baz.getTopFoos().iterator().next() != null);
assertTrue(baz.getStringSet().size() == 1);
assertTrue(baz.getBag().size() == 4);
assertTrue(baz.getFooToGlarch().size() == 2);
assertTrue(baz.getFooComponentToFoo().size() == 2);
assertTrue(baz.getGlarchToFoo().size() == 1);
Iterator iter = baz.getFooToGlarch().keySet().iterator();
for (int i = 0; i < 2; i++) assertTrue(iter.next() instanceof BarProxy);
FooComponent fooComp = (FooComponent) baz.getFooComponentToFoo().keySet().iterator().next();
assertTrue(((fooComp.getCount() == 123 && fooComp.getName().equals("name")) || (fooComp.getCount() == 12 && fooComp.getName().equals("nameName"))) && (baz.getFooComponentToFoo().get(fooComp) instanceof BarProxy));
Glarch g2 = new Glarch();
s.save(g2);
g = (GlarchProxy) baz.getTopGlarchez().get('G');
baz.getTopGlarchez().put('H', g);
baz.getTopGlarchez().put('G', g2);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
baz = (Baz) s.load(Baz.class, baz.getCode());
assertTrue(baz.getTopGlarchez().size() == 2);
assertTrue(baz.getCascadingBars().size() == 1);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
assertEquals(3, ((Long) s.createQuery("select count(*) from Bar").iterate().next()).longValue());
baz = (Baz) s.createQuery("select baz from Baz baz order by baz").list().get(0);
assertTrue(baz.getTopGlarchez().size() == 2);
assertTrue(baz.getCascadingBars().size() == 1);
txn.commit();
final Session s3 = (Session) SerializationHelper.deserialize(SerializationHelper.serialize(s));
s.close();
txn2 = s3.beginTransaction();
baz = (Baz) s3.load(Baz.class, baz.getCode());
assertEquals(3, ((Long) s3.createQuery("select count(*) from Bar").iterate().next()).longValue());
s3.delete(baz);
s3.delete(baz.getTopGlarchez().get('G'));
s3.delete(baz.getTopGlarchez().get('H'));
int rows = s3.doReturningWork(new AbstractReturningWork<Integer>() {
@Override
public Integer execute(Connection connection) throws SQLException {
final String sql = "update " + getDialect().openQuote() + "glarchez" + getDialect().closeQuote() + " set baz_map_id=null where baz_map_index='a'";
Statement st = connection.createStatement();
return st.executeUpdate(sql);
}
});
assertTrue(rows == 1);
assertEquals(2, doDelete(s3, "from Bar bar"));
FooProxy[] arr = baz.getFooArray();
assertTrue("new array of objects", arr.length == 4 && arr[1].getKey().equals(foo.getKey()));
for (int i = 1; i < arr.length; i++) {
if (arr[i] != null)
s3.delete(arr[i]);
}
//nonexistent
s3.load(Qux.class, new Long(666));
assertEquals(1, doDelete(s3, "from Glarch g"));
txn2.commit();
s3.disconnect();
Session s4 = (Session) SerializationHelper.deserialize(SerializationHelper.serialize(s3));
s3.close();
//s3.reconnect();
//nonexistent
assertTrue(s4.load(Qux.class, new Long(666)) != null);
//s3.disconnect();
s4.close();
}
Aggregations