Search in sources :

Example 1 with HSQLDialect

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());
    }
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) AssertionFailure(org.hibernate.AssertionFailure) DependantValue(org.hibernate.mapping.DependantValue) OneToMany(org.hibernate.mapping.OneToMany) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) Formula(org.hibernate.mapping.Formula) Random(java.util.Random) Selectable(org.hibernate.mapping.Selectable) Ejb3Column(org.hibernate.cfg.Ejb3Column) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(org.hibernate.mapping.Column) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) SimpleValue(org.hibernate.mapping.SimpleValue) DependantValue(org.hibernate.mapping.DependantValue) Value(org.hibernate.mapping.Value) ToOne(org.hibernate.mapping.ToOne) ManyToOne(org.hibernate.mapping.ManyToOne) Iterator(java.util.Iterator) AnnotationException(org.hibernate.AnnotationException) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty)

Example 2 with HSQLDialect

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;
}
Also used : MySQL5Dialect(org.hibernate.dialect.MySQL5Dialect) SybaseASE15Dialect(org.hibernate.dialect.SybaseASE15Dialect) H2Dialect(org.hibernate.dialect.H2Dialect) PostgreSQL92Dialect(org.hibernate.dialect.PostgreSQL92Dialect) IngresDialect(org.hibernate.dialect.IngresDialect) DerbyTenSixDialect(org.hibernate.dialect.DerbyTenSixDialect) Oracle10gDialect(org.hibernate.dialect.Oracle10gDialect) PostgreSQL94Dialect(org.hibernate.dialect.PostgreSQL94Dialect) FirebirdDialect(org.hibernate.dialect.FirebirdDialect) PostgreSQL82Dialect(org.hibernate.dialect.PostgreSQL82Dialect) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) PostgreSQL9Dialect(org.hibernate.dialect.PostgreSQL9Dialect) SybaseAnywhereDialect(org.hibernate.dialect.SybaseAnywhereDialect) DerbyTenSevenDialect(org.hibernate.dialect.DerbyTenSevenDialect) DB2400Dialect(org.hibernate.dialect.DB2400Dialect) Ingres9Dialect(org.hibernate.dialect.Ingres9Dialect) HSQLDialect(org.hibernate.dialect.HSQLDialect) HANAColumnStoreDialect(org.hibernate.dialect.HANAColumnStoreDialect) PostgresPlusDialect(org.hibernate.dialect.PostgresPlusDialect) DerbyDialect(org.hibernate.dialect.DerbyDialect) SQLServerDialect(org.hibernate.dialect.SQLServerDialect) Oracle9iDialect(org.hibernate.dialect.Oracle9iDialect) DerbyTenFiveDialect(org.hibernate.dialect.DerbyTenFiveDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) Ingres10Dialect(org.hibernate.dialect.Ingres10Dialect) SQLServer2012Dialect(org.hibernate.dialect.SQLServer2012Dialect) Oracle12cDialect(org.hibernate.dialect.Oracle12cDialect) SQLServer2005Dialect(org.hibernate.dialect.SQLServer2005Dialect) InformixDialect(org.hibernate.dialect.InformixDialect) CUBRIDDialect(org.hibernate.dialect.CUBRIDDialect) SQLServer2008Dialect(org.hibernate.dialect.SQLServer2008Dialect)

Example 3 with HSQLDialect

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();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Session(org.hibernate.Session) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) Test(org.junit.Test)

Example 4 with HSQLDialect

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();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) DerbyDialect(org.hibernate.dialect.DerbyDialect) List(java.util.List) ArrayList(java.util.ArrayList) DB2Dialect(org.hibernate.dialect.DB2Dialect) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with HSQLDialect

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();
}
Also used : SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Time(java.sql.Time) MySQLDialect(org.hibernate.dialect.MySQLDialect) TreeSet(java.util.TreeSet) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HSQLDialect(org.hibernate.dialect.HSQLDialect) Statement(java.sql.Statement) Connection(java.sql.Connection) PointbaseDialect(org.hibernate.dialect.PointbaseDialect) TreeMap(java.util.TreeMap) SAPDBDialect(org.hibernate.dialect.SAPDBDialect) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

HSQLDialect (org.hibernate.dialect.HSQLDialect)24 Session (org.hibernate.Session)22 Test (org.junit.Test)22 Transaction (org.hibernate.Transaction)15 MySQLDialect (org.hibernate.dialect.MySQLDialect)15 Iterator (java.util.Iterator)9 List (java.util.List)9 DB2Dialect (org.hibernate.dialect.DB2Dialect)8 ArrayList (java.util.ArrayList)7 PointbaseDialect (org.hibernate.dialect.PointbaseDialect)7 SAPDBDialect (org.hibernate.dialect.SAPDBDialect)6 Serializable (java.io.Serializable)4 Query (org.hibernate.Query)4 DerbyDialect (org.hibernate.dialect.DerbyDialect)4 MckoiDialect (org.hibernate.dialect.MckoiDialect)4 PostgreSQL81Dialect (org.hibernate.dialect.PostgreSQL81Dialect)4 SybaseDialect (org.hibernate.dialect.SybaseDialect)4 TimesTenDialect (org.hibernate.dialect.TimesTenDialect)4 ScrollableResults (org.hibernate.ScrollableResults)3 Oracle8iDialect (org.hibernate.dialect.Oracle8iDialect)3