Results 1 to 4 of 4
  1. #1
    berlindutza is offline Member
    Join Date
    Aug 2009
    Rep Power

    Default Difficulties using Hibernate + MySQL

    I'm having my fisrt attempt to use Hibernate with a Mysql database. I'm just trying to insert a row into a table. When the code is run, no errors appear, but the following text appears in the console (and nothing is inserted into the database):

    Inserting Record
    Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL) values (?, ?, ?)

    Shouldn't the question marks be replaced with the values specified in the main class?
    The code with which I created the database is:
    Create table CONTACT
    (ID int not null auto_increment primary key,
    FIRSTNAME varchar(50),
    LASTNAME varchar (50,
    EMAIL varchar (50));

    Can you suggest some errors in the code that follows?

    The persistence class

    package pack1;

    public class Contact {

    private String firstName;
    private String lastName;
    private String email;
    private long id;

    public Contact(){}
    public String getEmail() {
    return email;

    public String getFirstName() {
    return firstName;

    public String getLastName() {
    return lastName;

    public void setEmail(String string) {
    email = string;

    public void setFirstName(String string) {
    firstName = string;

    public void setLastName(String string) {
    lastName = string;

    public long getId() {
    return id;

    private void setId(long l) {
    id = l;

    The xml with which I configured Hibernate

    <property name="hibernate.connection.driver_class">org.gjt.m m.mysql.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/hello</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect </property>

    <property name="">update</property>

    <!-- Mapping files -->
    <mapping resource="contact.hbm.xml"/>


    Mapping the Contact Object to the Database Contact table


    <class name="pack1.Contact" table="CONTACT">

    <id name="id" type="long" column="ID" >
    <generator class="native"/>

    <property name="firstName">
    <column name="FIRSTNAME" />

    <property name="lastName">
    <column name="LASTNAME"/>

    <property name="email">
    <column name="EMAIL"/>



    The main class

    public class FirstExample {
    public static void main(String[] args) {
    Session session = null;

    // This step will read hibernate.cfg.xml and prepare hibernate for use

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    session =sessionFactory.openSession();

    //Create new instance of Contact and set values in it by reading them from form object
    System.out.println("Inserting Record");

    Contact contact = new Contact();


    }catch(Exception e){
    // Actual contact insertion will happen at this step



  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Rep Power


    Please use code tags next time when posting code.
    Try using
    Java Code:
    after opening the session and
    Java Code:
    after the save.

  3. #3
    berlindutza is offline Member
    Join Date
    Aug 2009
    Rep Power


    Thank you!
    It works:cool:!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    Just as an addition, the '?' are part of the original query, and they don't get replaced. What happens to them when Hibernate does its JDBC call is entirely dependent on the Driver used. Hibernate does not replace them, and most drivers pass that query through to the db, along with the parameters.

Similar Threads

  1. Difficulties randomizing images in an array
    By phb5004 in forum New To Java
    Replies: 2
    Last Post: 12-11-2009, 05:45 AM
  2. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 02:57 AM
  3. Difficulties logging onto Sun Developer's Network
    By jocassid in forum New To Java
    Replies: 0
    Last Post: 07-01-2008, 02:02 AM
  4. JOptionPane Display Difficulties
    By kewlgeye in forum New To Java
    Replies: 7
    Last Post: 05-09-2008, 08:09 PM
  5. Replies: 2
    Last Post: 06-30-2007, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts