spring 3.1.0 Release Hibernate 4.1.2 Final - 通过注释的数据库配置如何设置属性 [英] spring 3.1.0 Release Hibernate 4.1.2 Final - Database configuration via annotation how to set properties
问题描述
我正在使用下面列出的代码进行配置。
自动创建/删除功能接缝不会像应该那样工作(它不会创建/维护数据库中的表(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(){
返回新的LocalSessionFactoryBuilder(datasource())。addAnnotatedClasses(Account.class,Contact.class).buildSessionFactory();
$ b $Bean
public PlatformTransactionManager transactionManager(){
return new HibernateTransactionManager(sessionFactory());
@Bean
公共属性hibernateProperties(){
属性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);
返回属性; (){
}
@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());
返回dataSource;
}
}
帐户
@Entity @Table(name =T_ACCOUNT)
公共类帐户{
@Id
私人长ID;
@Column
私人双重cashBalance;
@Column
私人字符串名称;
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(){
returnid:+ id +,balance:+ cashBalance +,name: +名称;
$ b b
$ b 测试班
@RequestMapping(value =/ test,method = RequestMethod.GET)
public String homeTest(Model model){
Account account = new帐户();
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);
返回test;
解决方案属性作为数据源的连接属性。
@Bean
public SessionFactory sessionFactory(){
返回新的LocalSessionFactoryBuilder(datasource())
.addAnnotatedClasses(Account.class,Contact.class)
.addProperties(hibernateProperties())
.buildSessionFactory();
}
请参阅 http://docs.jboss.org/hibernate/core/3.6/javadocs/org/ hibernate / cfg / Configuration.html?is-external = true (LocalSessionFactoryBuilder的父类)
I am using configuration as in the code listed below.
auto create/drop function seams not to work as it should (it does not create/maintain tables in the database (dataSource.setConnectionProperties(hibernateProperties());))
(It works when table is already created in the DB? I think that properties are not taken into consideration here?),
Config
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;
}
}
Account
@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;
}
test class
@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";
}
解决方案 You're passing your hibernate properties as connection properties for your datasource. Instead, they should be passed to the sessionfactory.
@Bean
public SessionFactory sessionFactory() {
return new LocalSessionFactoryBuilder(datasource())
.addAnnotatedClasses(Account.class, Contact.class)
.addProperties(hibernateProperties())
.buildSessionFactory();
}
See http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/cfg/Configuration.html?is-external=true (parent class for LocalSessionFactoryBuilder)
这篇关于spring 3.1.0 Release Hibernate 4.1.2 Final - 通过注释的数据库配置如何设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!