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

    Default Difficulties using Hibernate + MySQL

    Hi!
    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 (?, ?, ?)
    Done



    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

    <hibernate-configuration>
    <session-factory>
    <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="hibernate.hbm2ddl.auto">update</property>

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

    </session-factory>
    </hibernate-configuration>




    Mapping the Contact Object to the Database Contact table


    <hibernate-mapping>

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

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

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

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

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

    </class>

    </hibernate-mapping>



    The main class

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

    try{
    // 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();

    //contact.setId(3);
    contact.setFirstName("Deepak");
    contact.setLastName("Kumar");
    contact.setEmail("deepak_38@yahoo.com");

    session.save(contact);

    System.out.println("Done");
    }catch(Exception e){
    System.out.println(e.getMessage());
    }finally{
    // Actual contact insertion will happen at this step
    session.flush();
    session.close();

    }

    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Please use code tags next time when posting code.
    Try using
    Java Code:
    session.beginTransaction();
    after opening the session and
    Java Code:
    session.getTransaction().commit();
    after the save.

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

    Default

    Thank you!
    It works:cool:!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    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, 04:45 AM
  2. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01: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
  •