コード例 #1
0
ファイル: TestEmbed.java プロジェクト: rullyz/nest
  private AbstractPersistentBeanDescriptor createOperateAuditInfoDescriptor(
      PersistentBeanDescriptorContext context) {
    EmbeddablePersistentBeanDescriptor descriptor = new EmbeddablePersistentBeanDescriptor();
    descriptor.setBeanClass(OperateAuditInfo.class);

    List<IPropertyDescriptor> properties = new ArrayList<IPropertyDescriptor>();
    {
      PersistentBeanPropertyDescriptor property = new PersistentBeanPropertyDescriptor();
      property.setName("createUserId");
      PrimitivePersistentColumn column = new PrimitivePersistentColumn();
      column.setName("CREATE_USER_ID");
      column.setType(PrimitiveColumnType.LONG);
      property.setPersistentColumn(column);
      properties.add(property);
    }

    {
      PersistentBeanPropertyDescriptor property = new PersistentBeanPropertyDescriptor();
      property.setName("createTime");
      PrimitivePersistentColumn column = new PrimitivePersistentColumn();
      column.setName("CREATE_TIME");
      column.setType(PrimitiveColumnType.TIMESTAMP);
      property.setPersistentColumn(column);
      properties.add(property);
    }

    descriptor.setProperties(properties);
    context.register(descriptor);
    return descriptor;
  }
コード例 #2
0
ファイル: TestEmbedOverridden.java プロジェクト: rullyz/nest
  private AbstractPersistentBeanDescriptor createPersonDescriptor(
      PersistentBeanDescriptorContext context) {
    StandalonePersistentBeanDescriptor descriptor = new StandalonePersistentBeanDescriptor();
    descriptor.setBeanClass(Person.class);
    descriptor.setTableName("T_PERSON");

    List<IPropertyDescriptor> properties = new ArrayList<IPropertyDescriptor>();
    {
      PersistentBeanPropertyDescriptor property = new PersistentBeanPropertyDescriptor();
      property.setName("id");
      PrimitivePersistentColumn column = new PrimitivePersistentColumn();
      column.setName("PERSON_ID");
      column.setType(PrimitiveColumnType.LONG);
      column.setPrimaryKey(true);
      HiloKey key = new HiloKey();
      column.setPrimaryKeyGenerator(key);
      property.setPersistentColumn(column);
      properties.add(property);
    }

    {
      PersistentBeanPropertyDescriptor property = new PersistentBeanPropertyDescriptor();
      property.setName("name");
      PrimitivePersistentColumn column = new PrimitivePersistentColumn();
      column.setName("PERSON_NAME");
      column.setType(PrimitiveColumnType.STRING);
      property.setPersistentColumn(column);
      properties.add(property);
    }

    {
      PersistentBeanPropertyDescriptor property = new PersistentBeanPropertyDescriptor();
      property.setName("operateAuditInfo");
      EmbeddedPersistentColumn column = new EmbeddedPersistentColumn();
      column.setEmbeddedBeanClass(OperateAuditInfo.class);
      column.addOverriddenColumnName("createUserId", "CREATE_USER_ID");
      column.addOverriddenColumnName("createTime", "CREATE_TIME");
      column.setPropertyDescriptor(property);
      property.setPersistentColumn(column);
      properties.add(property);
    }

    descriptor.setProperties(properties);
    context.register(descriptor);
    return descriptor;
  }
コード例 #3
0
ファイル: TestEmbed.java プロジェクト: rullyz/nest
  @Test
  public void test() throws ClassNotFoundException, SQLException {
    BasicConfigurator.configure();
    Logger.getRootLogger().setLevel(Level.FATAL);
    Logger.getLogger(HibernatePersistentConfigurationInitializer.class).setLevel(Level.DEBUG);

    {
      Class.forName("org.hsqldb.jdbc.JDBCDriver");
      Connection conn =
          DriverManager.getConnection("jdbc:hsqldb:mem:memdb", "username", "password");
      Statement stat = conn.createStatement();
      stat.execute(
          "create table T_PERSON(PERSON_ID BIGINT, PERSON_NAME VARCHAR(20), CREATE_USER_ID BIGINT, CREATE_TIME TIMESTAMP)");
      stat.execute("create table HIBERNATE_UNIQUE_KEY(NEXT_HI INT)");
      stat.execute("INSERT INTO HIBERNATE_UNIQUE_KEY(NEXT_HI) VALUES (1)");
      conn.commit();
      System.out.println("create TABLE:person OK");
      conn.close();
    }

    PersistentBeanDescriptorContext context = new PersistentBeanDescriptorContext();
    HibernatePersistentConfigurationInitializer initializer =
        new HibernatePersistentConfigurationInitializer();
    initializer.addProperty("hibernate.connection.driver_class", "org.hsqldb.jdbc.JDBCDriver");
    initializer.addProperty("hibernate.connection.url", "jdbc:hsqldb:mem:memdb");
    initializer.addProperty("hibernate.connection.username", "username");
    initializer.addProperty("hibernate.connection.password", "password");
    initializer.addProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    initializer.addProperty(
        "hibernate.cache.provider_class", "org.hibernate.cache.internal.NoCacheProvider");
    initializer.addProperty("hibernate.show_sql", "true");
    initializer.addProperty("hibernate.connection.pool_size", "1");
    initializer.addProperty("hibernate.current_session_context_class", "thread");
    context.addConfigurationInitializer(initializer);
    context
        .getOperatorProviderRegistry()
        .register(IPersistentBeanSaver.CODE, new HibernatePersistentSaverProvider());

    AbstractPersistentBeanDescriptor descriptor = createPersonDescriptor(context);
    createOperateAuditInfoDescriptor(context);
    context.afterContextInitialized();

    Person person = new Person();
    person.setName("test name");
    OperateAuditInfo info = new OperateAuditInfo();
    info.setCreateUserId(100l);
    info.setCreateTime(new Timestamp(new Date().getTime()));
    person.setOperateAuditInfo(info);

    IPersistentConfiguration configuration =
        context.getInitializedData(IPersistentConfigurationInitializer.KEY);
    SessionFactory sessionFactory = configuration.getRealConfiguration();
    sessionFactory.getCurrentSession().beginTransaction();
    descriptor.getSaver().save(person);
    sessionFactory.getCurrentSession().getTransaction().commit();

    {
      Connection conn =
          DriverManager.getConnection("jdbc:hsqldb:mem:memdb", "username", "password");
      Statement stat = conn.createStatement();
      ResultSet rst = stat.executeQuery("select * from T_PERSON");
      if (rst.next()) {
        assertEquals(101, rst.getLong("person_id"));
        assertEquals("test name", rst.getString("person_name"));
        assertEquals(100, rst.getLong("CREATE_USER_ID"));
        assertNotNull(rst.getTimestamp("CREATE_TIME"));
      }
      System.out.println("create TABLE:person OK");
      conn.close();
    }
  }