Ich habe ein einfaches Beispiel abgeschrieben und erhalte eine SQL GrammerException... Ich habe den MySQLConnector 5.1 im ClassPath und die DB Läuft. Was mach ich falsch?
Meine Config:
Das Bean:
und die "main":
Ach ja, dazu kommt noch HibernateUtil:
Meine 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">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">none</property>
<mapping class="Employee"/>
</session-factory>
</hibernate-configuration>
Das Bean:
Code:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
public Employee() {
}
@Id
@Column(name = "id")
Integer id;
@Column(name = "name")
String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
und die "main":
Code:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class Main {
public static void main(String[] args) throws Exception {
/** Getting the Session Factory and session */
SessionFactory session = HibernateUtil.getSessionFactory();
Session sess = session.getCurrentSession();
/** Starting the Transaction */
Transaction tx = sess.beginTransaction();
/** Creating Pojo */
Employee pojo = new Employee();
pojo.setId(new Integer(5));
pojo.setName("XYZ");
/** Saving POJO */
sess.save(pojo);
/** Commiting the changes */
tx.commit();
System.out.println("Record Inserted");
/** Closing Session */
session.close();
}
}
Ach ja, dazu kommt noch HibernateUtil:
Code:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}