On first glance, it looks good.
You might want to get rid of the annotation:
@GeneratedValue(strategy = GenerationType.AUTO)
MySQL will default to Identity. Or, perhaps you should make it identity, rather than AUTO?
Maybe instead of opensession, you should try getSession?
Check out my signature links for a vareity of Hibernate examples, all of which were tested and documented using MySQL. It might be a good reference tutorial for you.
Here's the JavaDoc for the Hibernate Session for your perusal:
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html
This is the code I use for the free Hibernate3 tutorials and JPA examples on my website. This is tested against MySQL. See if you notice any major differences. It is also a User class:
http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=03addingrecordswithhibernate
Code:
package com.examscam.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
@Entity
public class User {
private Long id;
private String password;
@Id
@GeneratedValue
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public String getPassword() {return password;}
public void setPassword(String password) {
this.password = password;
}
public static void main(String args[]){
AnnotationConfiguration config =
new AnnotationConfiguration();
config.addAnnotatedClass(User.class);
config.configure();
// new SchemaExport(config).create(true, true);
SessionFactory factory =
config.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
System.out.println("creating user");
User u = new User();
u.setPassword("abc123");
session.saveOrUpdate(u);
System.out.println("user saved");
session.getTransaction().commit();
System.out.println("transaction successful!!!");
}
}
http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=03addingrecordswithhibernate
Please post back and let us know what changes fixed your code. It will help people in a similar situation. Also, if you have more problems, let us know! Don't fret too much getting started with Hiberante.