@Before
  public void setUp() throws Exception {
    serviceRegistry =
        new StandardServiceRegistryBuilder()
            .enableAutoClose()
            .applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop")
            .build();

    MetadataBuildingContext buildingContext =
        new MetadataBuildingContextTestingImpl(serviceRegistry);

    Properties properties = new Properties();
    properties.setProperty(SequenceGenerator.SEQUENCE, TEST_SEQUENCE);
    properties.setProperty(SequenceHiLoGenerator.MAX_LO, "3");
    properties.put(
        PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
        buildingContext.getObjectNameNormalizer());

    generator = new SequenceHiLoGenerator();
    generator.configure(
        StandardBasicTypes.LONG, properties, serviceRegistry.getService(JdbcEnvironment.class));

    Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
    generator.registerExportables(metadata.getDatabase());

    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
  }
Esempio n. 2
0
  private static SessionFactory init() {
    StandardServiceRegistry standardRegistry =
        new StandardServiceRegistryBuilder().configure().build();
    Metadata metadata =
        new MetadataSources(standardRegistry)
            .addAnnotatedClass(Actor.class)
            .addAnnotatedClass(Movie.class)
            .buildMetadata();

    return metadata.buildSessionFactory();
  }
  @Test
  public void testGeneratedUuidId() {
    StandardServiceRegistry ssr =
        new StandardServiceRegistryBuilder()
            .applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop")
            .build();
    try {
      Metadata metadata =
          new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata();
      ((MetadataImpl) metadata).validate();

      PersistentClass entityBinding = metadata.getEntityBinding(TheEntity.class.getName());
      assertEquals(UUID.class, entityBinding.getIdentifier().getType().getReturnedClass());
      IdentifierGenerator generator =
          entityBinding
              .getIdentifier()
              .createIdentifierGenerator(
                  metadata.getIdentifierGeneratorFactory(),
                  metadata.getDatabase().getDialect(),
                  null,
                  null,
                  (RootClass) entityBinding);
      assertTyping(UUIDGenerator.class, generator);

      // now a functional test
      SessionFactory sf = metadata.buildSessionFactory();
      try {
        TheEntity theEntity = new TheEntity();

        Session s = sf.openSession();
        s.beginTransaction();
        s.save(theEntity);
        s.getTransaction().commit();
        s.close();

        assertNotNull(theEntity.id);

        s = sf.openSession();
        s.beginTransaction();
        s.delete(theEntity);
        s.getTransaction().commit();
        s.close();
      } finally {
        try {
          sf.close();
        } catch (Exception ignore) {
        }
      }
    } finally {
      StandardServiceRegistryBuilder.destroy(ssr);
    }
  }
  @Before
  public void setUp() throws Exception {
    BasicTestingJdbcServiceImpl jdbcServices = new BasicTestingJdbcServiceImpl();
    jdbcServices.prepare(false);

    serviceRegistry =
        new StandardServiceRegistryBuilder()
            .enableAutoClose()
            .addService(JdbcEnvironment.class, jdbcServices.getJdbcEnvironment())
            .addService(JdbcServices.class, jdbcServices)
            .applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop")
            .build();

    generator = new SequenceHiLoGenerator();

    // Build the properties used to configure the id generator
    Properties properties = new Properties();
    properties.setProperty(SequenceGenerator.SEQUENCE, TEST_SEQUENCE);
    properties.setProperty(SequenceHiLoGenerator.MAX_LO, "0"); // JPA allocationSize of 1
    properties.put(
        PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
        new ObjectNameNormalizer() {
          @Override
          protected MetadataBuildingContext getBuildingContext() {
            return new MetadataBuildingContextTestingImpl(serviceRegistry);
          }
        });
    generator.configure(StandardBasicTypes.LONG, properties, serviceRegistry);

    final Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
    metadata
        .getDatabase()
        .addAuxiliaryDatabaseObject(
            new SimpleAuxiliaryDatabaseObject(
                Collections.<String>emptySet(),
                null,
                null,
                generator.sqlCreateStrings(TestingDatabaseInfo.DIALECT),
                generator.sqlDropStrings(TestingDatabaseInfo.DIALECT)));

    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
  }