Hello,
When I run the following Program i get different results when the database runs on Windows and on Linux. The program is very simple. It creates users and lists them. The outcome of the programs are exactly the same. though the generated databases are difrent. (note the cases on table names and eventually on column names) The program also throws unhandled exceptions when the database is running on a linux machine.
Why does the database on linux generate this error?
Is the hibernate implementation using diffrent versions for mysql on linux and on windows?
Is it some sort of setting in mysql or hibernate?
any help is much appriciated...
thanx in advance
- Xander
----------- General info ---------------
Hibernate version: 3.1.3
Name and version of the database you are using:
Linux: MySQL 5.0.34
Windows MySQL 5.0.37
------ Windows ------------
Code:
mysql> use nieuwetest
Database changed
mysql> describe user;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| ID | bigint(20) | NO | PRI | NULL | auto_increment |
| ENABLED | tinyint(4) | NO | | | |
| USERNAME | varchar(255) | NO | UNI | | |
| EMAIL | varchar(255) | NO | | | |
| FIRST_NAME | varchar(255) | NO | | | |
| LAST_NAME | varchar(255) | NO | | | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.04 sec)
mysql> show tables;
+----------------------+
| Tables_in_nieuwetest |
+----------------------+
| user |
+----------------------+
1 row in set (0.00 sec)
----------- Linux ---------------
Code:
mysql> use nieuwetest;
Database changed
mysql> describe user;
ERROR 1146 (42S02): Table 'nieuwetest.user' doesn't exist
mysql> describe USER;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| ID | bigint(20) | NO | PRI | NULL | auto_increment |
| ENABLED | tinyint(4) | NO | | | |
| USERNAME | varchar(255) | NO | UNI | | |
| EMAIL | varchar(255) | NO | | | |
| FIRST_NAME | varchar(255) | NO | | | |
| LAST_NAME | varchar(255) | NO | | | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
mysql> show tables;
+----------------------+
| Tables_in_nieuwetest |
+----------------------+
| USER |
+----------------------+
1 row in set (0.00 sec)
----------- Stack trace when running on linux 2nd time ---------------
Code:
15:49:05,747 ERROR JDBCExceptionReporter:72 - Unknown database 'NIEUWETEST'
15:49:05,749 ERROR SchemaUpdate:165 - could not complete schema update
org.hibernate.exception.SQLGrammarException: could not get table metadata: USER
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.tool.hbm2ddl.DatabaseMetadata.getTableMetadata(DatabaseMetadata.java:100)
at org.hibernate.cfg.Configuration.generateSchemaUpdateScript(Configuration.java:838)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:140)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:296)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at nl.ap400.test.hibernate.InitSessionFactory.initSessionFactory(InitSessionFactory.java:45)
at nl.ap400.test.hibernate.InitSessionFactory.getInstance(InitSessionFactory.java:20)
at nl.ap400.test.hibernate.MainApp.createUser(MainApp.java:53)
at nl.ap400.test.hibernate.MainApp.main(MainApp.java:25)
Caused by: java.sql.SQLException: Unknown database 'NIEUWETEST'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2926)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2972)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:822)
at com.mysql.jdbc.DatabaseMetaData$9.forEach(DatabaseMetaData.java:4453)
at com.mysql.jdbc.DatabaseMetaData$IterateBlock.doForAll(DatabaseMetaData.java:79)
at com.mysql.jdbc.DatabaseMetaData.getTables(DatabaseMetaData.java:4440)
at com.mysql.jdbc.DatabaseMetaData$2.forEach(DatabaseMetaData.java:1928)
at com.mysql.jdbc.DatabaseMetaData$IterateBlock.doForAll(DatabaseMetaData.java:79)
at com.mysql.jdbc.DatabaseMetaData.getColumns(DatabaseMetaData.java:1894)
at org.hibernate.tool.hbm2ddl.TableMetadata.initColumns(TableMetadata.java:177)
at org.hibernate.tool.hbm2ddl.TableMetadata.<init>(TableMetadata.java:33)
at org.hibernate.tool.hbm2ddl.DatabaseMetadata.getTableMetadata(DatabaseMetadata.java:85)
... 8 more
CODE & CONFIG----------- Hibernate config ---------------
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost:3306/nieuwetest
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<property name="show_sql">true</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="nl/ap400/test/hibernate/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
---------- User.hbm.xml -----------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Attention: Generated code! Do not modify by hand!
Generated by: hibernate.hbm.xml.vsl in andromda-hibernate-cartridge.
-->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="none">
<class name="nl.ap400.test.hibernate.User" table="USER" dynamic-insert="false" dynamic-update="false">
<id name="id" type="java.lang.Long" unsaved-value="null">
<column name="ID" sql-type="BIGINT"/>
<generator class="native">
</generator>
</id>
<property name="enabled" type="boolean">
<column name="ENABLED" not-null="true" unique="false" sql-type="TINYINT"/>
</property>
<property name="username" type="java.lang.String">
<column name="USERNAME" not-null="true" unique="false" unique-key="usernameUnique" sql-type="VARCHAR(255) BINARY"/>
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" not-null="true" unique="false" sql-type="VARCHAR(255) BINARY"/>
</property>
<property name="firstName" type="java.lang.String">
<column name="FIRST_NAME" not-null="true" unique="false" sql-type="VARCHAR(255) BINARY"/>
</property>
<property name="lastName" type="java.lang.String">
<column name="LAST_NAME" not-null="true" unique="false" sql-type="VARCHAR(255) BINARY"/>
</property>
</class>
</hibernate-mapping>
---------- User.java ----------
Code:
package nl.ap400.test.hibernate;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -5356167726657927602L;
private java.lang.Long id;
private java.lang.String username;
private java.lang.String password;
private java.lang.String firstName;
private java.lang.String lastName;
private java.lang.String email;
private boolean enabled;
public boolean equals(Object object) {
if (this == object)
{
return true;
}
if (!(object instanceof User))
{
return false;
}
final User that = (User)object;
if (this.id == null || that.getId() == null || !this.id.equals(that.getId()))
{
return false;
}
return true;
}
public String toString() {
return "@" + id + " " + firstName + " " + lastName;
}
public java.lang.String getEmail() {
return email;
}
public boolean isEnabled() {
return enabled;
}
public java.lang.String getFirstName() {
return firstName;
}
public java.lang.Long getId() {
return id;
}
public java.lang.String getLastName() {
return lastName;
}
public java.lang.String getPassword() {
return password;
}
public java.lang.String getUsername() {
return username;
}
public void setEmail(java.lang.String email) {
this.email = email;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setFirstName(java.lang.String firstName) {
this.firstName = firstName;
}
public void setId(java.lang.Long id) {
this.id = id;
}
public void setLastName(java.lang.String lastName) {
this.lastName = lastName;
}
public void setPassword(java.lang.String password) {
this.password = password;
}
public void setUsername(java.lang.String username) {
this.username = username;
}
}
--------------- MainApp.java -----------------
Code:
package nl.ap400.test.hibernate;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
public class MainApp {
private static Logger log = Logger.getLogger(MainApp.class);
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
User user = new User();
user.setEmail("my@email.com");
user.setFirstName("xander");
user.setLastName("wiseGuy");
user.setEnabled(true);
user.setPassword("secret");
user.setUsername("xander_"+i);
createUser(user);
}
listUser();
}
private static void listUser() {
Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
List Users = session.createQuery("select u from User as u").list();
for (Iterator iter = Users.iterator(); iter.hasNext();) {
User element = (User) iter.next();
log.info(element);
}
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
}
private static void createUser(User User) {
Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
session.save(User);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
}
}
-------------------- InitSessionFactory.java ---------------------
Code:
package nl.ap400.test.hibernate;
import javax.naming.InitialContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
public class InitSessionFactory {
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
public static SessionFactory getInstance() {
if (sessionFactory == null) initSessionFactory();
return sessionFactory;
}
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
public static void close(){
if (sessionFactory != null) sessionFactory.close();
sessionFactory = null;
}
private static synchronized void initSessionFactory() {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
String sessionFactoryJndiName = cfg.getProperty(Environment.SESSION_FACTORY_NAME);
if (sessionFactoryJndiName != null) {
cfg.buildSessionFactory();
sessionFactory = (SessionFactory) (new InitialContext())
.lookup(sessionFactoryJndiName);
} else{
sessionFactory = cfg.buildSessionFactory();
}
} catch (Exception ex) {
throw new HibernateException("Could not initialize the Hibernate configuration", ex);
}
}
}
}
----------------- log4j.properties ------------------------
Code:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.logger.org.hibernate=info
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.type=info
log4j.logger.org.hibernate.tool.hbm2ddl=info
log4j.logger.org.hibernate.cache=info