几天前我开始使用Spring,由于某种原因,我无法让Hibernate自动创建表实体(我已经在其他项目中使用了几次hibernate,所以我知道它有这种能力)。我该如何解决?我附上了一些可能有帮助的代码。
HibernateConfiguration.class
@Configuration
@EnableTransactionManagement
@ComponentScan("br.com.jeffersontpadua.spring.config")
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("br.com.jeffersontpadua.spring.model");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DriverManagerDataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.hbm2dll.auto", environment.getRequiredProperty("hibernate.hbm2dll.auto"));
System.out.println("Hibernate config: " + environment.getRequiredProperty("hibernate.hbm2dll.auto"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
}
application.properties
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/coaching_works
jdbc.username = root
jdbc.password = 1234
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.format_sql = true
hibernate.show_sql = true
hibernate.hbm2dll.auto = update
Costumer.java
@Entity
@Table(name = "costumer")
public class Costumer {
@Id
@GeneratedValue
@Column(name = "id", nullable = false, updatable = false, unique = true)
private int id;
@Column(name = "full_name", nullable = false, length = 160)
private String fullName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
答案 0 :(得分:2)
你有一个错字:该属性被称为hibernate.hbm2ddl.auto
(.. ddl .. not ..dll ..)