Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring 3.1.0发布Hibernate 4.1.2最终版-通过注释配置数据库如何设置属性_Java_Spring_Hibernate_Hibernate Annotations - Fatal编程技术网

Java spring 3.1.0发布Hibernate 4.1.2最终版-通过注释配置数据库如何设置属性

Java spring 3.1.0发布Hibernate 4.1.2最终版-通过注释配置数据库如何设置属性,java,spring,hibernate,hibernate-annotations,Java,Spring,Hibernate,Hibernate Annotations,我正在使用下面列出的代码中的配置。 自动创建/删除函数无法正常工作(它不会在数据库(dataSource.setConnectionProperties(hibernateProperties());)中创建/维护表) (当数据库中已经创建了表时,它就可以工作了?我认为这里没有考虑属性?) 配置 package com.parisibw.persistance; import java.util.Properties; import javax.sql.DataSource; import

我正在使用下面列出的代码中的配置。 自动创建/删除函数无法正常工作(它不会在数据库(dataSource.setConnectionProperties(hibernateProperties());)中创建/维护表)

(当数据库中已经创建了表时,它就可以工作了?我认为这里没有考虑属性?)

配置

package com.parisibw.persistance;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.parisibw.forms.Contact;

@Configuration
@EnableTransactionManagement
public class HibernateConfig {


    @Bean
    public SessionFactory sessionFactory() {
     return new LocalSessionFactoryBuilder(datasource()).addAnnotatedClasses(Account.class, Contact.class).buildSessionFactory();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new HibernateTransactionManager(sessionFactory());
    }

    @Bean
    public Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.put("hibernate.show_sql", "true");       
        properties.put("hibernate.hbm2ddl.auto", "create");

        //properties.put("hibernate.connection.driver_class", "org.h2.Driver");
        //properties.put("hibernate.connection.url", "jdbc:h2:db/test;CIPHER=AES");
        //properties.put("hibernate.connection.username", "root");
        //properties.put("hibernate.connection.password", "root root");
        //properties.put("hibernate.connection.pool_size", "1");        
        //properties.put("hibernate.format_sql", "true");
        //properties.put("hibernate.use_sql_comments", "true");
        //properties.put("hibernate.c3p0.min_size", "5");
        //properties.put("hibernate.c3p0.max_size", "20");
        //properties.put("hibernate.c3p0.timeout", "300");
        //properties.put("hibernate.c3p0.max_statements", "50");
        //properties.put("hibernate.c3p0.idle_test_period", "3000");
        //properties.put("hibernate.cache.use_second_level_cache", "true");
        //properties.put("hibernate.cache.region.factory_class",
        //"org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
        //properties.put("hibernate.cache.use_query_cache", "true");
        //properties.put("hibernate.cache.use_minimal_puts", "true");
        //properties.put("hibernate.max_fetch_depth", "10");        

        return properties;
    }

    @Bean
    public DataSource datasource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://dbpath");
            dataSource.setUsername("username");
            dataSource.setPassword("password");
            dataSource.setConnectionProperties(hibernateProperties());
            return dataSource;
    }

}
帐目

@Entity @Table(name="T_ACCOUNT")
public class Account {

    @Id 
    private long id;
    @Column
    private double cashBalance;
    @Column
    private String name;

    public long getId() {
          return id;
    }
    public void setId(long id) {
          this.id = id;
    }
    public double getCashBalance() {
          return cashBalance;
    }
    public void setCashBalance(double cashBalance) {
        this.cashBalance = cashBalance;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return  "id: " + id + ", balance: " + cashBalance + ", name: " + name;

    }
测试班

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String homeTest(Model model) {
    Account account = new Account();

    try{    
        sessionFactory = new HibernateConfig().sessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    //account.setId(2);
    account.setName("Marcin");
    account.setCashBalance(1200);
    session.save(account);
    session.getTransaction().commit();
    session.close();

    }catch (Exception e) {
        logger.info(e.toString());
    }

    model.addAttribute("serverTime" );
    return "test";
}

我在这里看到的第一个问题是,您显式地创建了一个新的HibernateConfig,而不是允许spring自动连接它。因此,在测试类中添加以下属性:

@Autowire
private SessionFactory sessionFactory;
更深入地看,你可能想读一点关于Spring和IOC的知识,因为你用一种奇怪的方式做这件事,看起来你可能有点困惑。你要做的每件事实际上都是在Spring中构建的,所以你要重新发明轮子。简言之,您正在创建新的事物实例,而不是从spring的上下文中获取它们,所以现在它有了一种方法来为您连接事物


看看这里是如何实现的(是的,它还有其他问题,但您应该能够得到一般的想法):

您正在将hibernate属性作为数据源的连接属性传递。相反,它们应该传递给sessionfactory

@Bean
public SessionFactory sessionFactory() {
 return new LocalSessionFactoryBuilder(datasource())
   .addAnnotatedClasses(Account.class, Contact.class)
   .addProperties(hibernateProperties())
   .buildSessionFactory();
}

请参阅(LocalSessionFactoryBuilder的父类)

上面的配置文件是我配置所有Hibernate属性的地方(我不使用xml文件或属性文件来配置Hibernate)。@main他使用的是一个bean配置类。这是一种使用Spring而不使用xml的相对较新的方法。这并不奇怪。
buildSessionFactory()
不推荐使用
addProperties()
。使用不推荐的方法会有任何问题吗?@TheKojuEffect no;只要它还在那里,它就应该工作。这只是意味着你应该开始转移到另一个版本,因为它将在未来的版本中被删除。通常,不推荐使用的注释会提到要使用的替代方法。您是否熟悉任何替代方法,或者使用
buildSessionFactory(ServiceRegistry ServiceRegistry)
@kojueffect我认为现在的主要替代方法是根本不使用SessionFactory,但是JPA EntityManager是实现持久性的java标准方法。Hibernate是它的一个实现;日食也是如此。换句话说,如果使用JPA,可以在hibernate或eclipselink之间切换作为实现。请看,这里有一个不错的nough教程:。对于其他人来说,谷歌是你的朋友。这个评论部分很难再深入下去了。