Java 未生成Spring+hibernate表

Java 未生成Spring+hibernate表,java,mysql,spring,hibernate,jpa,Java,Mysql,Spring,Hibernate,Jpa,我的表格没有生成,我觉得我遗漏了一些明显的东西。使用MySQL,所有属性都正确映射到会话工厂 据我所知,考虑到这些设置,它应该可以工作,对吗?我丢了什么东西吗 应用程序属性 com.example.springmvc.configuration.HibernateConfiguration.java com.example.springmvc.model.Employee 原木的好部分。这可能表明会话工厂有问题 11:18:01.908 [RMI TCP Connection(2)-127.0.

我的表格没有生成,我觉得我遗漏了一些明显的东西。使用MySQL,所有属性都正确映射到会话工厂

据我所知,考虑到这些设置,它应该可以工作,对吗?我丢了什么东西吗

应用程序属性

com.example.springmvc.configuration.HibernateConfiguration.java

com.example.springmvc.model.Employee

原木的好部分。这可能表明会话工厂有问题

11:18:01.908 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/dc_comp?autoReconnect=true&useSSL=false]
11:18:02.068 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator - Database ->
       name : MySQL
    version : 5.7.20-0ubuntu0.16.04.1
      major : 5
      minor : 7
11:18:02.068 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator - Driver ->
       name : MySQL Connector Java
    version : mysql-connector-java-5.1.44 ( Revision: b3cda4f864902ffdde495b9df93937c3e20009be )
      major : 5
      minor : 1
...
11:18:02.152 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.boot.model.relational.Namespace - Created database namespace [logicalName=Name{catalog=null, schema=null}, physicalName=Name{catalog=null, schema=null}]
11:18:02.169 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.example.springmvc.model.Employee
11:18:02.188 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3DiscriminatorColumn{logicalColumnName'DTYPE', discriminatorTypeName='string'}
11:18:02.193 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.EntityBinder - Import with entity name Employee
...
11:18:02.195 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.EntityBinder - Bind entity com.example.springmvc.model.Employee on table EMPLOYEE
11:18:02.218 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(EMPLOYEE), mappingColumn=id, insertable=true, updatable=true, unique=false}
11:18:02.221 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.PropertyBinder - MetadataSourceProcessor property id with lazy=false
11:18:02.223 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.AbstractPropertyHolder - Attempting to locate auto-apply AttributeConverter for property [com.example.springmvc.model.Employee:id]
11:18:02.225 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.SimpleValueBinder - building SimpleValue for id
11:18:02.227 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.PropertyBinder - Building property id
11:18:02.230 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(EMPLOYEE), mappingColumn=NAME, insertable=true, updatable=true, unique=false}
11:18:02.230 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.PropertyBinder - MetadataSourceProcessor property name with lazy=false
11:18:02.230 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.AbstractPropertyHolder - Attempting to locate auto-apply AttributeConverter for property [com.example.springmvc.model.Employee:name]
11:18:02.231 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.SimpleValueBinder - building SimpleValue for name
11:18:02.231 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.PropertyBinder - Building property name
...
11:18:02.240 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.SimpleValueBinder - Starting fillSimpleValue for id
11:18:02.240 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.SimpleValueBinder - Starting fillSimpleValue for name
...
11:18:02.241 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.mapping.PrimaryKey - Forcing column [id] to be non-null as it is part of the primary key for table [employee]
...
11:34:55.533 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.Settings - SessionFactory name : null
...
11:34:55.570 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.internal.SessionFactoryImpl - Returning a Reference to the SessionFactory
11:34:55.571 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.internal.SessionFactoryImpl - Session factory constructed with filter configurations : {}
11:34:55.571 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.internal.SessionFactoryImpl - Instantiating session factory with properties: {java.vendor=Oracle Corporation, sun.java.launcher=SUN_STANDARD, ...
...
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'dc_comp.EMPLOYEE' doesn't exist
我已经在这上面呆了几个小时了,任何帮助都将不胜感激

提前谢谢大家,


CX

如果您的表当前不存在,并且希望Spring/Hibernate在启动时创建它们,则需要更改 spring.jpa.hibernate.ddl-auto=更新到spring.jpa.hibernate.ddl-auto=创建
有关详细信息:

是否尝试更新或create@hurricane是的,在我的application.properties中有spring.jpa.generate-ddl=true和spring.jpa.hibernate.ddl-auto=update,在方法HibernateConfiguration.hibernateProperties中设置为hibernate属性,然后将其设置为会话工厂sessionFactory.SetHibernateProperties hibernateProperties;我认为您需要使用:properties.puthbernate.hbm2ddl.auto、environment.getRequiredPropertyspring.jpa.hibernate.ddl-auto@飓风解决了我的问题,出于某种原因,spring.jpa.hibernate.ddl-auto无法工作,我必须直接使用hibernate属性,正如您所提到的。这两个选项都已经出现在application.properties中,但spring.jpa.hibernate.ddl-auto由于某种原因无法坚持。用hibernate.hbm2ddl.auto替换它解决了我的问题。我认为spring.jpa.hibernate版本用于在使用spring引导时进行配置。如果未使用Spring Boot,则使用标准hibernate属性,如上所述。
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.example.springmvc.configuration"})
@EntityScan({"com.example.springmvc.model"})
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(hibernateDataSource());
        sessionFactory.setPackagesToScan(new String[] {"com.example.springmvc.model"});
        sessionFactory.setHibernateProperties(hibernateProperties());
        sessionFactory.afterPropertiesSet();
        return sessionFactory;
    }

    @Bean
    public DataSource hibernateDataSource() {
        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;
    }

    public 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("spring.jpa.generate-ddl", environment.getRequiredProperty("spring.jpa.generate-ddl"));

        properties.put("spring.jpa.hibernate.ddl-auto", environment.getRequiredProperty("spring.jpa.hibernate.ddl-auto"));
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(s);
        return txManager;
    }
}
@Entity
@Table(name="EMPLOYEE")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Size(min=3, max=50)
    @Column(name = "NAME", nullable = false)
    private String name;

    ...
}
11:18:01.908 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/dc_comp?autoReconnect=true&useSSL=false]
11:18:02.068 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator - Database ->
       name : MySQL
    version : 5.7.20-0ubuntu0.16.04.1
      major : 5
      minor : 7
11:18:02.068 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator - Driver ->
       name : MySQL Connector Java
    version : mysql-connector-java-5.1.44 ( Revision: b3cda4f864902ffdde495b9df93937c3e20009be )
      major : 5
      minor : 1
...
11:18:02.152 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.boot.model.relational.Namespace - Created database namespace [logicalName=Name{catalog=null, schema=null}, physicalName=Name{catalog=null, schema=null}]
11:18:02.169 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.example.springmvc.model.Employee
11:18:02.188 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3DiscriminatorColumn{logicalColumnName'DTYPE', discriminatorTypeName='string'}
11:18:02.193 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.EntityBinder - Import with entity name Employee
...
11:18:02.195 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.EntityBinder - Bind entity com.example.springmvc.model.Employee on table EMPLOYEE
11:18:02.218 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(EMPLOYEE), mappingColumn=id, insertable=true, updatable=true, unique=false}
11:18:02.221 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.PropertyBinder - MetadataSourceProcessor property id with lazy=false
11:18:02.223 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.AbstractPropertyHolder - Attempting to locate auto-apply AttributeConverter for property [com.example.springmvc.model.Employee:id]
11:18:02.225 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.SimpleValueBinder - building SimpleValue for id
11:18:02.227 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.PropertyBinder - Building property id
11:18:02.230 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(EMPLOYEE), mappingColumn=NAME, insertable=true, updatable=true, unique=false}
11:18:02.230 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.PropertyBinder - MetadataSourceProcessor property name with lazy=false
11:18:02.230 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.AbstractPropertyHolder - Attempting to locate auto-apply AttributeConverter for property [com.example.springmvc.model.Employee:name]
11:18:02.231 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.SimpleValueBinder - building SimpleValue for name
11:18:02.231 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.PropertyBinder - Building property name
...
11:18:02.240 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.SimpleValueBinder - Starting fillSimpleValue for id
11:18:02.240 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.annotations.SimpleValueBinder - Starting fillSimpleValue for name
...
11:18:02.241 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.mapping.PrimaryKey - Forcing column [id] to be non-null as it is part of the primary key for table [employee]
...
11:34:55.533 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.cfg.Settings - SessionFactory name : null
...
11:34:55.570 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.internal.SessionFactoryImpl - Returning a Reference to the SessionFactory
11:34:55.571 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.internal.SessionFactoryImpl - Session factory constructed with filter configurations : {}
11:34:55.571 [RMI TCP Connection(2)-127.0.0.1] DEBUG org.hibernate.internal.SessionFactoryImpl - Instantiating session factory with properties: {java.vendor=Oracle Corporation, sun.java.launcher=SUN_STANDARD, ...
...
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'dc_comp.EMPLOYEE' doesn't exist