org.hibernate.cfg.Configuration.addAnnotatedClass()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(222)

本文整理了Java中org.hibernate.cfg.Configuration.addAnnotatedClass()方法的一些代码示例,展示了Configuration.addAnnotatedClass()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration.addAnnotatedClass()方法的具体详情如下:
包路径:org.hibernate.cfg.Configuration
类名称:Configuration
方法名:addAnnotatedClass

Configuration.addAnnotatedClass介绍

[英]Read metadata from the annotations associated with this class.
[中]从与此类关联的注释中读取元数据。

代码示例

代码示例来源:origin: dropwizard/dropwizard

private void addAnnotatedClasses(Configuration configuration,
                   Iterable<Class<?>> entities) {
    final SortedSet<String> entityClasses = new TreeSet<>();
    for (Class<?> klass : entities) {
      configuration.addAnnotatedClass(klass);
      entityClasses.add(klass.getCanonicalName());
    }
    LOGGER.info("Entity classes: {}", entityClasses);
  }
}

代码示例来源:origin: hibernate/hibernate-orm

private SessionFactoryImplementor buildSessionFactory(List<Class<?>> classesUnderTest, List<String> configFiles) {
  assert classesUnderTest != null;
  assert configFiles != null;
  Configuration cfg = new Configuration();
  for ( Class<?> clazz : classesUnderTest ) {
    cfg.addAnnotatedClass( clazz );
  }
  for ( String configFile : configFiles ) {
    try ( InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( configFile ) ) {
      cfg.addInputStream( is );
    }
    catch (IOException e) {
      throw new IllegalArgumentException( e );
    }
  }
  return ( SessionFactoryImplementor ) cfg.buildSessionFactory();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  @TestForIssue( jiraKey = "HHH-8167" )
  public void testAnnotatedFormWithBeanValidationNotNull() {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass( AnnotatedMaster.class ).addAnnotatedClass( AnnotatedDetail.class );
    cfg.buildSessionFactory().close();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Before
public void buildSessionFactory() {
  Configuration cfg = new Configuration()
      .addAnnotatedClass( TheEntity.class );
  cfg.getProperties().put( AvailableSettings.DIALECT, NoTupleSupportDialect.class.getName() );
  cfg.getProperties().put( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
  sessionFactory = cfg.buildSessionFactory();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
@TestForIssue( jiraKey = "ANN-718" )
public void testBackquotes() {
  try {
    Configuration config = new Configuration();
    config.addAnnotatedClass(Bug.class);
    config.addAnnotatedClass(Category.class);
    sessionFactory = config.buildSessionFactory( serviceRegistry );
  }
  catch( Exception e ) {
    StringWriter writer = new StringWriter();
    e.printStackTrace(new PrintWriter(writer));
    log.debug(writer.toString());
    fail(e.getMessage());
  }
  finally {
    if ( sessionFactory != null ) {
      sessionFactory.close();
      sessionFactory = null;
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@TransactionAttribute(TransactionAttributeType.NEVER)
public void start() {
  try {
    Configuration configuration = new Configuration();
    configuration = configuration.configure( "hibernate.cfg.xml" );
    configuration.addAnnotatedClass( WildFlyDdlEntity.class );
    sessionFactory = configuration.buildSessionFactory();
  }
  catch (Throwable ex) {
    System.err.println( "Initial SessionFactory creation failed." + ex );
    throw new ExceptionInInitializerError( ex );
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void testInitialization() throws Exception {
    Configuration configuration = new Configuration();
    configuration.addAnnotatedClass( A.class );
    configuration.addAnnotatedClass( B.class );
    configuration.addAnnotatedClass( C.class );
    StandardServiceRegistryImpl serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( configuration.getProperties() );
    try {
      SessionFactory sessionFactory = configuration.buildSessionFactory( serviceRegistry );
      sessionFactory.close();
    }
    finally {
      serviceRegistry.destroy();
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

public void start() {
  Configuration configuration = new Configuration();
  configuration = configuration.configure( "hibernate.cfg.xml" );
  configuration.addAnnotatedClass( WildFlyDdlEntity.class );
  // creating the SF should run schema creation
  sessionFactory = configuration.buildSessionFactory();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void testAnnReferencesHbm() throws Exception {
    Configuration cfg = new Configuration();
    cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
    cfg.addAnnotatedClass( Port.class );
    cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
    SessionFactory sf = cfg.buildSessionFactory();
    assertNotNull( sf );
    Session s = sf.openSession();
    Transaction tx = s.beginTransaction();
    Query q = s.createQuery( "from Boat" );
    assertEquals( 0, q.list().size() );
    q = s.createQuery( "from Port" );
    assertEquals( 0, q.list().size() );
    tx.commit();
    s.close();
    sf.close();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Before
public void beforeTest() {
  Configuration cfg = new Configuration()
      .addAnnotatedClass( Item.class )
      .setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
  cfg.addAuxiliaryDatabaseObject( ProcedureDefinitions.INSTANCE );
  sf = cfg.buildSessionFactory();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testIlikeMimicing() {
  SessionFactory sf = new Configuration()
      .addAnnotatedClass( IrrelevantEntity.class )
      .setProperty( AvailableSettings.DIALECT, NonIlikeSupportingDialect.class.getName() )
      .setProperty( Environment.HBM2DDL_AUTO, "create-drop" )
      .buildSessionFactory();
  try {
    final Criteria criteria = sf.openSession().createCriteria( IrrelevantEntity.class );
    final CriteriaQueryTranslator translator = new CriteriaQueryTranslator(
        (SessionFactoryImplementor) sf,
        (CriteriaImpl) criteria,
        IrrelevantEntity.class.getName(),
        "a"
    );
    final Criterion ilikeExpression = Restrictions.ilike( "name", "abc" );
    final String ilikeExpressionSqlFragment = ilikeExpression.toSqlString( criteria, translator );
    assertEquals( "lowLowLow(a.name) like ?", ilikeExpressionSqlFragment );
  }
  finally {
    sf.close();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testIlikeRendering() {
  SessionFactory sf = new Configuration()
      .addAnnotatedClass( IrrelevantEntity.class )
      .setProperty( AvailableSettings.DIALECT, IlikeSupportingDialect.class.getName() )
      .setProperty( Environment.HBM2DDL_AUTO, "create-drop" )
      .buildSessionFactory();
  try {
    final Criteria criteria = sf.openSession().createCriteria( IrrelevantEntity.class );
    final CriteriaQueryTranslator translator = new CriteriaQueryTranslator(
        (SessionFactoryImplementor) sf,
        (CriteriaImpl) criteria,
        IrrelevantEntity.class.getName(),
        "a"
    );
    final Criterion ilikeExpression = Restrictions.ilike( "name", "abc" );
    final String ilikeExpressionSqlFragment = ilikeExpression.toSqlString( criteria, translator );
    assertEquals( "a.name insensitiveLike ?", ilikeExpressionSqlFragment );
  }
  finally {
    sf.close();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void testDuplicateEntityName() throws Exception {
    Configuration cfg = new Configuration();
    cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
    ServiceRegistry serviceRegistry = null;
    SessionFactory sf = null;
    try {
      cfg.addAnnotatedClass( Flight.class );
      cfg.addAnnotatedClass( org.hibernate.test.annotations.Flight.class );
      cfg.addAnnotatedClass( org.hibernate.test.annotations.Company.class );
      cfg.addResource( "org/hibernate/test/annotations/orm.xml" );
      cfg.addResource( "org/hibernate/test/annotations/duplicatedgenerator/orm.xml" );
      serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
      sf = cfg.buildSessionFactory( serviceRegistry );
      Assert.fail( "Should not be able to map the same entity name twice" );
    }
    catch (DuplicateMappingException ae) {
      //success
    }
    finally {
      if (sf != null){
        sf.close();
      }
      if ( serviceRegistry != null ) {
        ServiceRegistryBuilder.destroy( serviceRegistry );
      }
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void testWrongOneToOne() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass( Show.class )
        .addAnnotatedClass( ShowDescription.class );
    cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
    ServiceRegistry serviceRegistry = null;
    SessionFactory sessionFactory = null;
    try {
      serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );
      sessionFactory = cfg.buildSessionFactory( serviceRegistry );
      Assert.fail( "Wrong mappedBy does not fail property" );
    }
    catch (AnnotationException e) {
      //success
    }
    finally {
      if(sessionFactory!=null){
        sessionFactory.close();
      }
      if ( serviceRegistry != null ) {
        ServiceRegistryBuilder.destroy( serviceRegistry );
      }

    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void testPartialResults() {
    Configuration cfg = new Configuration()
        .addAnnotatedClass( Employee.class )
        .setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
    cfg.addAuxiliaryDatabaseObject( new ProcedureDefinition() );
    SessionFactory sf = cfg.buildSessionFactory();
    try {
      Session session = sf.openSession();
      session.beginTransaction();

      ProcedureCall call = session.createStoredProcedureCall( "allEmployeeNames", "id-fname-lname" );
      ProcedureOutputs outputs = call.getOutputs();
      ResultSetOutput output = assertTyping( ResultSetOutput.class, outputs.getCurrent() );
      assertEquals( 3, output.getResultList().size() );
      assertTyping( Employee.class, output.getResultList().get( 0 ) );

      session.getTransaction().commit();
      session.close();
    }
    finally {
      sf.close();
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testPrecedenceHbm() throws Exception {
  Configuration cfg = new Configuration();
  cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
  cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
  cfg.addAnnotatedClass( Boat.class );
  SessionFactory sf = cfg.buildSessionFactory();
  assertNotNull( sf );
  Session s = sf.openSession();
  s.getTransaction().begin();
  Boat boat = new Boat();
  boat.setSize( 12 );
  boat.setWeight( 34 );
  s.persist( boat );
  s.getTransaction().commit();
  s.clear();
  Transaction tx = s.beginTransaction();
  boat = (Boat) s.get( Boat.class, boat.getId() );
  assertTrue( "Annotation has precedence", 34 != boat.getWeight() );
  s.delete( boat );
  //s.getTransaction().commit();
  tx.commit();
  s.close();
  sf.close();
}
 @Test

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testPrecedenceAnnotation() throws Exception {
  Configuration cfg = new Configuration();
  cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
  cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
  cfg.setProperty( Configuration.ARTEFACT_PROCESSING_ORDER, "class, hbm" );
  cfg.addAnnotatedClass( Boat.class );
  SessionFactory sf = cfg.buildSessionFactory();
  assertNotNull( sf );
  Session s = sf.openSession();
  s.getTransaction().begin();
  Boat boat = new Boat();
  boat.setSize( 12 );
  boat.setWeight( 34 );
  s.persist( boat );
  s.getTransaction().commit();
  s.clear();
  Transaction tx = s.beginTransaction();
  boat = (Boat) s.get( Boat.class, boat.getId() );
  assertTrue( "Annotation has precedence", 34 == boat.getWeight() );
  s.delete( boat );
  tx.commit();
  s.close();
  sf.close();
}
 @Test

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Tests that nothing crazy happens if the hibernate-envers jar happens
 * to be on the classpath but we have no audited entities
 */
@Test
@TestForIssue( jiraKey = "HHH-9675" )
public void testNoAudited() {
  new Configuration().buildSessionFactory().close();
  new Configuration().addAnnotatedClass( SimpleNonAuditedEntity.class ).buildSessionFactory().close();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  @TestForIssue( jiraKey = "HHH-7446" )
  public void testUniqueConstraintAnnotationOnNaturalIds() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
    configuration.addAnnotatedClass(Month.class);
    SessionFactory sf = null;
    try {
      sf = configuration.buildSessionFactory();
      sf.close();
    }
    catch (ConcurrentModificationException e) {
      fail(e.toString()); 
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void testDeclarativeMix() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass( IncorrectEntity.class );
    cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
    ServiceRegistry serviceRegistry = null;
    SessionFactory sessionFactory = null;
    try {
      serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
      sessionFactory = cfg.buildSessionFactory( serviceRegistry );
      fail( "Entity wo id should fail" );
    }
    catch (AnnotationException e) {
      //success
    }
    finally {
      if( sessionFactory !=null){
        sessionFactory.close();
      }
      if ( serviceRegistry != null ) {
        ServiceRegistryBuilder.destroy( serviceRegistry );
      }
    }
  }
}

相关文章

Configuration类方法