2013-08-02 2 views
0

xml 구성 파일 대신 java를 통해 구성 할 때 어떻게 최대 절전 모드 테이블 리소스를 매핑합니까? 최대 절전 모드 리소스에 대한java 설정을 통해 최대 절전 테이블 엔티티 매핑

XML 매핑 :

<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 

     <property name="dataSource"> 
      <ref bean="dataSource"/> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.SybaseDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
     <property name="mappingResources"> 
     <list> 
       <value>/config/Stock.hbm.xml</value> 
     </list> 
     </property> 
    </bean> 

자바 매핑?

@Configuration 
@EnableTransactionManagement 
@PropertySource({ "classpath:persistence-sql.properties" }) 
@ComponentScan({......."}) 
public class PersistenceConfig { 

    @Autowired 
    private Environment env; 

    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
     sessionFactory.setDataSource(restDataSource()); 
     sessionFactory.setPackagesToScan(new String[] {...... }); 
     sessionFactory.setHibernateProperties(hibernateProperties()); 
    (HOW DO WE INTRODUCE mappingResources HERE?) 
     return sessionFactory; 
    } 

답변

0

@Entity가 모델 개체에서 문제를 해결했습니다. xml 구성 옵션에이 주석이 필요하지 않았습니다.

0

이 같은 런타임에 구성을 편집 할 수 있습니다

Configuration cfg = new Configuration() 
    .addResource("Item.hbm.xml") 
    .addResource("Bid.hbm.xml"); 

또는 같은 클래스의

: 여기

Configuration cfg = new Configuration() 
    .addClass(org.hibernate.auction.Item.class) 
    .addClass(org.hibernate.auction.Bid.class); 

reference here

+0

xml을 사용하지 않습니다 .I 컨텍스트를 다음과 같이로드하십시오. - AnnotationConfigApplicationContext ctx = \t \t \t AnnotationConfigApplicationContext(); \t \t ctx.register (PersistenceConfig.class); \t \t ctx.refresh(); – IUnknown

0

는 최소한의 노력 최대 절전 모드 + JPA입니다 관심이있는 설정 :

@Configuration 
@EnableTransactionManagement 
public class DaoConfiguration { 

    @Bean 
    public Properties hibernateProperties() { 
     Properties properties = new Properties(); 
     properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect"); 
     properties.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver"); 
     properties.setProperty(Environment.URL, "jdbc:hsqldb:mem:testdb"); 
     properties.setProperty(Environment.USER, "sa"); 
     properties.setProperty(Environment.PASS, ""); 
     properties.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "org.springframework.orm.hibernate4.SpringSessionContext"); 
     properties.setProperty(Environment.STATEMENT_BATCH_SIZE, "30"); 
     properties.setProperty(Environment.SHOW_SQL, "true"); 
     properties.setProperty(Environment.HBM2DDL_AUTO, "update"); 
     return properties; 
    } 

    @Bean 
    public HibernateEntityManagerFactory entityManagerFactory() { 
     Ejb3Configuration config = new Ejb3Configuration(); 
     return (HibernateEntityManagerFactory) config.addProperties(hibernateProperties()). 
       addAnnotatedClass(User.class). 
       buildEntityManagerFactory(); 
    } 

    @Bean 
    public SessionFactory sessionFactory() { 
     return entityManagerFactory().getSessionFactory(); 
    } 

    @Bean 
    public HibernateExceptionTranslator hibernateExceptionTranslator() { 
     return new HibernateExceptionTranslator(); 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager() { 
     return new JpaTransactionManager(entityManagerFactory()); 
    } 

}