2016-06-24 14 views
1

これは私の最初のHibernateの試行であり、設定に問題があります。私はMySQLを使用し、アプリケーションの開始時にテーブルを自動作成するように休止状態を設定しようとしています。問題は、Hibernate(v4.3.11)がMySQLの方言を使いたいと思っていることを正しく読んでいると思いますが、他の方言でコードを生成しています。アプリケーションの出力を見てください...Hibernate 4はMySQLの方言を無視しています

私が忘れてしまったことはありますか?ご協力いただきありがとうございます。

アプリケーションの出力あります:次のように

cze 24, 2016 11:39:01 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final} 
cze 24, 2016 11:39:01 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.3.11.Final} 
cze 24, 2016 11:39:01 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.connection.useUnicode=true, hibernate.format_sql=true, hibernate.connection.CharSet=UTF-8, hibernate.connection.username=root, hibernate.hbm2ddl.auto=create, hibernate.connection.url=jdbc:mysql://localhost/czymdojade?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8, hibernate.bytecode.use_reflection_optimizer=false, hibernate.show_sql=true, hibernate.connection.password=****, hibernate.connection.characterEncoding=UTF-8} 
cze 24, 2016 11:39:01 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
cze 24, 2016 11:39:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
cze 24, 2016 11:39:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/czymdojade?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8] 
cze 24, 2016 11:39:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH000046: Connection properties: {useUnicode=true, user=root, password=****, CharSet=UTF-8, characterEncoding=UTF-8} 
cze 24, 2016 11:39:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH000006: Autocommit mode: false 
cze 24, 2016 11:39:01 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 
cze 24, 2016 11:39:01 PM org.hibernate.dialect.Dialect <init> 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 
cze 24, 2016 11:39:01 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService 
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 
cze 24, 2016 11:39:01 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> 
INFO: HHH000397: Using ASTQueryTranslatorFactory 
cze 24, 2016 11:39:01 PM org.hibernate.tool.hbm2ddl.SchemaExport execute 
INFO: HHH000227: Running hbm2ddl schema export 
Hibernate: 
    drop table if exists lines 
cze 24, 2016 11:39:01 PM org.hibernate.tool.hbm2ddl.SchemaExport perform 
ERROR: HHH000389: Unsuccessful: drop table if exists lines 
cze 24, 2016 11:39:01 PM org.hibernate.tool.hbm2ddl.SchemaExport perform 
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lines' at line 1 
Hibernate: 
    create table lines (
     id integer not null auto_increment, 
     line varchar(255) not null, 
     url TEXT not null, 
     primary key (id) 
    ) 
cze 24, 2016 11:39:01 PM org.hibernate.tool.hbm2ddl.SchemaExport perform 
ERROR: HHH000389: Unsuccessful: create table lines (id integer not null auto_increment, line varchar(255) not null, url TEXT not null, primary key (id)) 
cze 24, 2016 11:39:01 PM org.hibernate.tool.hbm2ddl.SchemaExport perform 
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lines (
     id integer not null auto_increment, 
     line varchar(255) no' at line 1 
cze 24, 2016 11:39:01 PM org.hibernate.tool.hbm2ddl.SchemaExport execute 
INFO: HHH000230: Schema export complete 
Hibernate: 
    insert 
    into 
     lines 
     (line, url) 
    values 
     (?, ?) 
cze 24, 2016 11:39:02 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 
WARN: SQL Error: 1064, SQLState: 42000 
cze 24, 2016 11:39:02 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lines (line, url) values ('10', 'http://www.mpk.poznan.pl/component/transport/10' at line 1 
cze 24, 2016 11:39:02 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning 
WARN: SQL Warning Code: 1064, SQLState: 42000 
cze 24, 2016 11:39:02 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning 
WARN: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lines (line, url) values ('10', 'http://www.mpk.poznan.pl/component/transport/10' at line 1 
could not execute statement 

HibernateUtil.javaは:

package pl.rosiakit.hibernate; 

import org.hibernate.SessionFactory; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 

public class HibernateUtil 
{ 
    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() { 

     Configuration cfg=new Configuration(); 
     cfg.addAnnotatedClass(pl.rosiakit.hibernate.model.Line.class); 

     StandardServiceRegistryBuilder builder 
      = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()); 


     SessionFactory factory= cfg.buildSessionFactory(builder.build()); 

     //sessionFactory.setPackagesToScan(new String[] { "pl.rosiakit.model" }); 

     return factory; 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public static void shutdown() { 
     getSessionFactory().close(); 
    } 
} 

マイhibernate.properties

hibernate.connection.driver_class=com.mysql.jdbc.Driver 
hibernate.connection.url=jdbc:mysql://localhost/czymdojade?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8 
hibernate.connection.username=root 
hibernate.connection.password= 
hibernate.dialect=org.hibernate.dialect.MySQLDialect 
hibernate.show_sql=true 
hibernate.format_sql=true 
hibernate.hbm2ddl.auto=create 
hibernate.connection.CharSet=UTF-8 
hibernate.connection.characterEncoding=UTF-8 
hibernate.connection.useUnicode=true 
を3210

ラインモデル:

@Entity 
@Table(name = "lines") 
public class Line implements Serializable { 

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

    @Column(nullable = false) 
    private String line; 

    @Column(nullable = false) 
    private String url; 

    // getters and setters... 
} 

自分のアプリケーションの一部:

private int saveLine(String number, String url){ 

    Line line = new Line(); 
    line.setLine(number); 
    line.setUrl(url); 

    Session session = HibernateUtil.getSessionFactory().openSession(); 
    session.beginTransaction(); 

    int id = (Integer) session.save(line); 
    session.getTransaction().commit(); 
    session.close(); 
    return id; 
} 

答えて

2

"行は" MySQLでの予約語です。最も簡単な解決策は、テーブルの別の名前を選択することです。

+0

よろしくお願いいたします。それでおしまい :) –

関連する問題