-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem MySQL-Hibernate
PostPosted: Tue Jun 10, 2008 9:34 am 
Newbie

Joined: Thu Jun 05, 2008 9:47 am
Posts: 2
Good time.

I have problem with using Hibernate and MySQL.

I created simple application for showing this problem.

If configuration application to the MySQL database, then application create exception(please see further)

But, if configuration this application to the PostgreSQL database, application work fine.


Please help me

Code :

1. Main Action


public class Test {
public static void main(String[] args) {
Configuration cfg = new AnnotationConfiguration().addAnnotatedClass(
User.class).configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

System.out.println("Start action ...");

User user = new User();
session.saveOrUpdate(user);
System.out.println("Created user ...");

transaction.commit();
System.out.println("commit...");

}

}


2. User.java

@Entity
@Table(name = "new_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
}



3. Listener

public class PreListener implements PreInsertEventListener,PreDeleteEventListener, PreUpdateEventListener{

@Override
public boolean onPreInsert(PreInsertEvent event) {
save(event.getSource());
return false;
}

@Override
public boolean onPreDelete(PreDeleteEvent event) {
EntityPersister entityPersister = event.getPersister();
return false;
}

@Override
public boolean onPreUpdate(PreUpdateEvent preupdateevent) {
save(preupdateevent.getSource());
return false;
}


public void save(SessionImplementor source){
source.executeNativeUpdate(
new NativeSQLQuerySpecification("INSERT INTO annotation_history( id, value, size, command_id)" +
" VALUES( 0, 'wwwwwwwwwww', 0, 0)",
new NativeSQLQueryReturn[] {}, null),
new QueryParameters());
}
}


4. Hibernate.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- properties -->

<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3316/testDB</property>
<property name="hibernate.connection.username">testUser</property>
<property name="hibernate.connection.password">password</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<mapping class="User" />

<event type="pre-insert">
<listener class="com.listener.PreListener" />
</event>

<event type="pre-delete">
<listener class="com.listener.PreListener" />
</event>

<event type="pre-update">
<listener class="com.listener.PreListener" />
</event>


</session-factory>

</hibernate-configuration>


==============================================================

Problem : application create exception

Exception in thread "main" org.hibernate.AssertionFailure: null id in User entry (don't flush the Session after an exception occurs)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:55)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:164)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:120)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1158)
at com.listener.PreListener.save(PreListener.java:37)
at com.listener.PreListener.onPreInsert(PreListener.java:19)
at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:122)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:45)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at Test.main(Test.java:35)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 10:53 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
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.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.