use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.
the class GeneratedValueTests method baseline.
@Test
public void baseline() {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ExplicitGeneratorEntity.class).buildMetadata();
final PersistentClass entityMapping = bootModel.getEntityBinding(ExplicitGeneratorEntity.class.getName());
final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
final SequenceStyleGenerator sequenceStyleGenerator = assertTyping(SequenceStyleGenerator.class, generator);
assertThat(sequenceStyleGenerator.getDatabaseStructure().getName(), is("my_real_db_sequence"));
// all the JPA defaults since they were not defined
assertThat(sequenceStyleGenerator.getDatabaseStructure().getInitialValue(), is(100));
assertThat(sequenceStyleGenerator.getDatabaseStructure().getIncrementSize(), is(500));
}
use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.
the class GeneratedValueTests method testExplicitIncrementGenerator.
@Test
public void testExplicitIncrementGenerator() {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ExplicitIncrementGeneratorEntity.class).buildMetadata();
final PersistentClass entityMapping = bootModel.getEntityBinding(ExplicitIncrementGeneratorEntity.class.getName());
final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
assertTyping(IncrementGenerator.class, generator);
}
use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.
the class GeneratedValueTests method testImplicitSequenceGenerator.
@Test
public void testImplicitSequenceGenerator() {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME, "false").build();
final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ImplicitSequenceGeneratorEntity.class).buildMetadata();
final PersistentClass entityMapping = bootModel.getEntityBinding(ImplicitSequenceGeneratorEntity.class.getName());
final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
final SequenceStyleGenerator sequenceStyleGenerator = assertTyping(SequenceStyleGenerator.class, generator);
// PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME == false indicates that the legacy
// default (hibernate_sequence) should be used
assertThat(sequenceStyleGenerator.getDatabaseStructure().getName(), is("hibernate_sequence"));
// the JPA defaults since they were not defined
assertThat(sequenceStyleGenerator.getDatabaseStructure().getInitialValue(), is(1));
assertThat(sequenceStyleGenerator.getDatabaseStructure().getIncrementSize(), is(50));
}
use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.
the class GeneratedValueTests method testExplicitSequenceGeneratorImplicitName.
@Test
public void testExplicitSequenceGeneratorImplicitName() {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME, "false").build();
final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ExplicitSequenceGeneratorImplicitNameEntity.class).buildMetadata();
final PersistentClass entityMapping = bootModel.getEntityBinding(ExplicitSequenceGeneratorImplicitNameEntity.class.getName());
final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
final SequenceStyleGenerator sequenceStyleGenerator = assertTyping(SequenceStyleGenerator.class, generator);
// all the JPA defaults since they were not defined
assertThat(sequenceStyleGenerator.getDatabaseStructure().getName(), is(SequenceStyleGenerator.DEF_SEQUENCE_NAME));
assertThat(sequenceStyleGenerator.getDatabaseStructure().getInitialValue(), is(100));
assertThat(sequenceStyleGenerator.getDatabaseStructure().getIncrementSize(), is(500));
}
use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.
the class JoinColumnTest method testIt.
@Test
public void testIt() {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
try {
try (SessionFactoryImplementor sf = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(Company.class).addAnnotatedClass(Location.class).buildMetadata().buildSessionFactory()) {
try {
inTransaction(sf, session -> {
final Company acme = new Company(1, "Acme Corp");
new Location(1, "86-215", acme);
new Location(2, "20-759", acme);
session.persist(acme);
});
inTransaction(sf, session -> {
final Company acme = session.get(Company.class, 1);
assert acme.getLocations().size() == 2;
// this fails. however it is due to a number of bad assumptions
// in the TCK:
// First the spec says:
//
// {quote}
// The relationship modeling annotation constrains the use of the cascade=REMOVE specification. The
// cascade=REMOVE specification should only be applied to associations that are specified as One-
// ToOne or OneToMany. Applications that apply cascade=REMOVE to other associations are not por-
// table.
// {quote}
//
// Here the test is applying cascade=REMOVE to a ManyToOne.
//
// Secondly, the spec says:
//
// {quote}
// The persistence provider runtime is permitted to perform synchronization to the database at other times
// as well when a transaction is active and the persistence context is joined to the transaction.
// {quote}
//
//
// In other words, the provider is actually legal to perform the database delete immediately. Since
// the TCK deletes the Company first, a provider is legally able to perform the database delete on
// Company immediately. However the TCK test as defined makes this impossible:
// 1) simply deleting Company won't work since Location rows still reference it. Locations
// would need to be deleted first (cascade the remove to the @OneToMany `locations`
// attribute), or
// 2) perform a SQL update, updating the COMP_ID columns in the Location table to be null
// so that deleting Company wont cause FK violations. But again this is made impossible
// by the TCK because it defines the column as non-nullable (and not even in the @JoinColumn
// btw which would be another basis for challenge).
session.remove(acme);
for (Location location : acme.getLocations()) {
session.remove(location);
}
});
} finally {
inTransaction(sf, session -> {
session.createQuery("delete Location").executeUpdate();
session.createQuery("delete Company").executeUpdate();
});
}
}
} finally {
StandardServiceRegistryBuilder.destroy(ssr);
}
}
Aggregations