Reputation: 142
It's my first try Hibernate and I've got troubles with configuration. I'm trying to configure hibernate to use MySQL and autocreate tables on application start. The problem is that Hibernate (v4.3.11) correctly (I think) read that I want to use MySQL dialect, but it generate code in some other dialect. Please take a look at application output...
Is there something I've forgot? Thank you in advance for your help.
There's application output:
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 as follows:
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();
}
}
My 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
Line model:
@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...
}
Part of my application:
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;
}
Upvotes: 1
Views: 353
Reputation: 5558
"lines" is a reserved word in MySql. Easiest solution is to pick another name for the Table.
Upvotes: 2