java - hibernate 生成的表,但不显示在 mysql 数据库中

标签 java mysql spring hibernate

您好,我正在编写名为Master Spring MVC 的书的代码,我正在使用Mysql 数据库 而不是嵌入式数据库。在书中,作者使用了基于 xml 的配置,但我使用的是基于 java 的配置。到这里为止一切正常。

我面临的问题是,当我在 tomcat 服务器中运行我的代码并使用日志(用于特定表的表生成的 hibernate 日志)时,一切都很好,但表正在生成我的数据库,但不是这个名为 historic 的表。我附上了我的代码以及显示该表已生成的日志:

请注意,这是一个多模块项目,因此 webapp 和数据库是单独配置的。 1) Hibernate Log 生成特定表

类的 hibernate 日志:

create table historic (
   historic_type varchar(31) not null,
    id integer not null auto_increment,
    adj_close double precision,
    change_percent double precision,
    close double precision not null,
    from_date datetime,
    high double precision not null,
    interval varchar(255),
    low double precision not null,
    open double precision not null,
    to_date datetime,
    volume double precision not null,
    ask double precision,
    bid double precision,
    index_code varchar(255),
    stock_code varchar(255),
    primary key (id)
) engine=MyISAM

2)数据库配置文件

@Configuration
@ComponentScan("edu.zipcloud.cloudstreetmarket.core.entities")
@EnableTransactionManagement
public class JpaHibernateSpringConfig {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public DataSource dataSource() {
        logger.info("============================[Data Source Configuration: Began]");
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost/cloudstreet");
        ds.setUsername("root");
        ds.setPassword("root");

        logger.info("============================[Data Source Configuration: Ends]");
        return ds;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
        logger.info("============================[LocalContainerEntityManagerFactoryBean: Began]");
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setDataSource(dataSource());
        bean.setPersistenceUnitName("jpaData");
        bean.setPackagesToScan("edu.zipcloud.cloudstreetmarket.core");
        bean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        bean.setJpaProperties(hibernateJPAProperties());
        logger.info("===========================[LocalContainerEntityManagerFactoryBean: Ends]");
        return bean;
    }

    public Properties hibernateJPAProperties() {
        logger.info("============================[hibernateJPAProperties: Began]");
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");
        properties.put("hibernate.hbm2ddl.auto", "create");
        properties.put("hibernate.naming-strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
        properties.put("hibernate.default_schema", "public");

        logger.info("============================[hibernateJPAProperties: Ends]");
        return properties;
    }

    @Bean
    public JpaTransactionManager jpaTransactionManager() {
        logger.info("============================[jpaTransactionManager: Began]");
        JpaTransactionManager manager = new JpaTransactionManager();
        manager.setEntityManagerFactory(localContainerEntityManagerFactoryBean().getObject());
        logger.info("============================[jpaTransactionManager: Ends]");
        return manager;
    }
}

2) DispatcherServlet:

public class DispatcherServletConifg extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { JpaHibernateSpringConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebServletInit.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

3)历史实体

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "historic_type")
@Table(name = "historic")
public abstract class Historic implements Serializable {

    private static final long serialVersionUID = -802306391915956578L;

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

    private double open;

    private double high;

    private double low;

    private double close;

    private double volume;

    @Column(name = "adj_close")
    private double adjClose;

    @Column(name = "change_percent")
    private double changePercent;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "from_date")
    private Date fromDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "to_date")
    private Date toDate;

    @Enumerated(EnumType.STRING)
    @Column(name = "interval")
    private QuotesInterval interval;

请查看日志,如果您需要任何其他信息,请告诉我。

谢谢。

最佳答案

我认为问题是 mysql 关键字被用作列名“interval” 试试别的名字

我们可以像下面这样重命名间隔字段

@Enumerated(EnumType.STRING)
@Column(name = "quotesinterval")
private QuotesInterval interval;

任何符合您需要的名称。

底线是不应该使用任何 mysql keywords

关于java - hibernate 生成的表,但不显示在 mysql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45369651/

相关文章:

java - Spring 3.1 HandlerInterceptor 未被调用

java - 在 VS Code 中哪里设置 JUnit 的 vmArgs?

java - 发送无效的xml时如何修复websphere上的java堆空间

java - 使 Quartz 调度程序在没有 JDBC 的情况下持久化

MySQL 5.6 CAST 字符串到 Int 返回错误 1064

mysql - SQL 与非常大的表中列的最右边字符进行比较

php - MySQL 条件选择 2 个字段中的 1 个,然后按 2 个字段的组合分组

java连接池,多线程批处理中的最大连接数是多少?

Java:空值解析 XML 文件

Spring 启动 : How to add another WAR files to the embedded tomcat?