Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate query executes but nothing is added
    primarykey
    data
    text
    <p>I'm learning hibernate and when I run the following program, I get this message : <code>Hibernate: insert into contact (firstname, lastname, email, id) values (?, ?, ?, ?)</code> but when I check the table, nothing seems to be inserted. What's the problem here ? Clearly the statement value field is null but why?</p> <p>I have the following POJO:</p> <pre><code>public class Contact { private String firstName; private String lastName; private String email; private long id; /** * @return Email */ public String getEmail() { return email; } /** * @return First Name */ public String getFirstName() { return firstName; } /** * @return Last name */ public String getLastName() { return lastName; } /** * @param string Sets the Email */ public void setEmail(String string) { email = string; } /** * @param string Sets the First Name */ public void setFirstName(String string) { firstName = string; } /** * @param string sets the Last Name */ public void setLastName(String string) { lastName = string; } /** * @return ID Returns ID */ public long getId() { return id; } /** * @param l Sets the ID */ public void setId(long l) { id = l; } } </code></pre> <p>and below are my hibernate config and hbm files:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; &lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt; &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost/aneesh&lt;/property&gt; &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt; &lt;property name="hibernate.connection.password"&gt;123&lt;/property&gt; &lt;property name="hibernate.connection.pool_size"&gt;10&lt;/property&gt; &lt;property name="show_sql"&gt;true&lt;/property&gt; &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt; &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt; &lt;!-- Mapping files --&gt; &lt;mapping resource="contact.hbm.xml"/&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; &lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="Contact" table="contact"&gt; &lt;id name="id" column="id" &gt; &lt;generator class="assigned"/&gt; &lt;/id&gt; &lt;property name="firstName" &gt; &lt;column name="firstname" /&gt; &lt;/property&gt; &lt;property name="lastName"&gt; &lt;column name="lastname"/&gt; &lt;/property&gt; &lt;property name="email" &gt; &lt;column name="email"/&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>and here is my code to insert :</p> <pre><code>import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.Session; public class HSession { public static void main(String[] args) { Session session = null; try{ SessionFactory sessionGet = new Configuration().configure().buildSessionFactory(); session = sessionGet.openSession(); Contact contact = new Contact(); contact.setFirstName("xyz"); contact.setLastName("zyx"); contact.setEmail("x@xmail.com"); contact.setId(20); session.save(contact); }finally{ // Actual contact insertion will happen at this step try { session.flush(); session.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload