这是我第一次尝试Hibernate而且我遇到了配置方面的麻烦。我试图配置hibernate以使用MySQL并在应用程序启动时自动创建表。问题是Hibernate(v4.3.11)正确(我认为)读到我想使用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
线型:
@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;
}
答案 0 :(得分:2)
“lines”是MySql中的保留字。最简单的解决方案是为表格选择另一个名称。