表由hibernate生成但未在mysql数据库中显示

时间:2017-07-28 09:21:39

标签: java mysql spring hibernate

您好我正在处理名为Master Spring MVC的书籍代码,我正在使用Mysql Database代替embeded database。书中作者使用了基于xml的配置,但我使用的是基于java的。直到这里一切正常。

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

请注意,这不是一个多模块项目,因此webapp,数据库是单独配置的。 1)Hibernate Log to genrerate specific table

类的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;

请查看日志,如果您需要任何其他信息,请与我们联系。

感谢。

3 个答案:

答案 0 :(得分:2)

我认为问题是mysql关键字被用作列名“interval” 尝试其他名称

我们可以像下面那样重命名区间字段

db.then((db) => {
    console.log('connected to db');
    const creators = db.get('creators');

    return fs.readdirAsync(folder)
    .catch(err => {
        exitOnErr(err, 'error reading directory');
    })
    .map(filename => {
        return processFile(filename)
        .then(content => {
            return Papa.parsePromise(content);
        })
        .then(results => {
            return db_add(results, creators);
        });
    });
}).catch((err) => {
    exitOnErr(err, 'error connecting to db');
}).then(() => {
    console.log('Completed parsing files...');
    closeAndExit(0);
});

OR

符合您需要的任何名称。

底线不应使用任何mysql keywords

答案 1 :(得分:0)

我按照Ashish的建议尝试生成的sql查询,结果是interval不支持字段类型,因此我稍微更改了字段名称并且它开始工作但仍然不确定是否不支持。配置应该抛出错误

我改变了@Enumerated(EnumType.STRING) @Column(name = "interval") private QuotesInterval interval;

@Enumerated(EnumType.STRING) @Column(name = "“间隔'") //<-- here private QuotesInterval interval;

@column()内部有轻微的勾号有效。 谢谢。

答案 2 :(得分:0)

什么对我有用... 验证实体名称。

仔细检查您在mysql中查询正确的表名。这发生在我身上,但是我一直在查询类名,而不是表名

 @Entity
 @Table(name= "Account") // select * from Account, not AccountDetails
 public class AccountDetails implements Serializable { /* class info */}