2017-07-28 17 views
1

嗨我正在處理代碼書名爲Master Spring MVC,我正在使用Mysql Database而不是embeded database。在書作者已經使用xml爲基礎的配置,但我使用基於Java。直到這裏一切工作正常。休眠表生成但在mysql數據庫中不顯示的表

我面臨的問題是,當我在tomcat服務器和日誌(特定表的表生成的休眠日誌)中運行我的代碼時,一切都很好,但表正在我的數據庫中生成但不是此表稱爲historic。我附上了我的代碼以及顯示錶生成的日誌:

請不要這是一個多模塊項目,因此webapp,數據庫是單獨配置的。 1)休眠登錄到genrerate特定表

休眠日誌類:

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)數據庫configuraton文件

@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; 

請看看日誌,如果你需要任何其他信息讓我知道。

謝謝。

回答

2

我認爲問題是MySQL的關鍵字被用作列名「間隔」 嘗試一些其他的名字

,我們可以重命名像間隔字段下面

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

OR

任何名稱,其符合您的需求。

底線是不應該使用任何mysql keywords

+0

你可以給你的答案增加實際的解決方案,所以我可以接受它 – user7036414

+0

我編輯了我的答案 – Ashish451

0

我試過的建議通過Ashish原來,interval沒有得到支持的字段類型生成的SQL查詢,所以我改變了我的字段名一點點,它開始工作,但仍不確定是否支持。配置應該扔了一個錯誤

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

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

有輕微@column()奏效。 謝謝。