-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate trying to create table instead of insert
PostPosted: Sat Sep 12, 2009 12:35 pm 
Newbie

Joined: Sat Sep 12, 2009 11:21 am
Posts: 5
I'm having problems putting eclipse+hibernate+postgre working.

These are my classes and mapping file:
Code:
public class Leitor {
  private String id;
  private int posX;
  private int posY;
  private int posZ;
  private String Nome;
 

  public String getNome() {
    return Nome;
  }
  public String getId() {
    return id;
  }
  public int getPosX() {
    return posX;
  }
  public int getPosY() {
    return posY;
  } 
  public int getPosZ() {
    return posZ;
  }
  public void setId(String string) {
    id = string;
  }
  public void setNome(String string) {
    Nome = string;
  }
  public void setPosX(int i) {
     posX = i;
  }
  public void setPosY(int i) {
     posY = i;
  }
  public void setPosZ(int i) {
     posZ = i;
  }
}


Code:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

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

       try{

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

         System.out.println("Inserting Record");
        
         Leitor leitor = new Leitor();
        
         leitor.setId("1");
         leitor.setPosX(90);
         leitor.setPosY(50);
         leitor.setPosZ(130);
         leitor.setNome("Leitor 1");
         session.save(leitor);
         System.out.println("Done");
       }catch(Exception e){
         System.out.println(e.getMessage());
       }finally{
        
         session.flush();
         session.close();
        }
      
     }
}


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="acessoBanco.Leitor" table="&quot;LEITOR&quot;" schema="&quot;SITRACK&quot;">
   <id name="id" length="15" type="string">
   <column name="&quot;ID&quot;" />
   <generator class="assigned"/>
  </id>
  <property name="posX" type="integer">
     <column name="&quot;POSICAO_X&quot;" />
  </property>
  <property name="posY" type="integer">
    <column name="&quot;POSICAO_Y&quot;"/>
  </property>
  <property name="posZ" type="integer">
    <column name="&quot;POSICAO_Z&quot;"/>
  </property>
  <property name="nome" length="50" type="string">
    <column name="&quot;NOME&quot;"/>
  </property> 
</class>
</hibernate-mapping>


It doesn't show any errors, but doesn't insert the values either.
Looking in the postgre log file i noticed this:

2009-09-12 13:33:34 BRT LOG: loaded library "$libdir/plugins/plugin_debugger.dll"
2009-09-12 13:33:35 BRT ERROR: relation "LEITOR" already exists
2009-09-12 13:33:35 BRT STATEMENT: create table "SITRACK"."LEITOR" ("ID" varchar(255) not null, "POSICAO_X" int4, "POSICAO_Y" int4, "POSICAO_Z" int4, "NOME" varchar(255), primary key ("ID"))
2009-09-12 13:33:35 BRT LOG: unexpected EOF on client connection

It's trying to create the table instead of inserting the values.
Does anybody knows whats happening?
Thank you.


Top
 Profile  
 
 Post subject: Re: Hibernate trying to create table instead of insert
PostPosted: Sun Sep 13, 2009 2:43 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I don't see any point where you begin or commit your transaction. I think that might be the problem.

_________________
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  
 
 Post subject: Re: Hibernate trying to create table instead of insert
PostPosted: Sun Sep 13, 2009 9:53 am 
Newbie

Joined: Sat Sep 12, 2009 11:21 am
Posts: 5
I've been told that I don't need to use transactions...that session.save and session.flush are enough. But clearly it's not working.
I'm really new in this hibernate world so I don't know how to use transaction. How would that code be?
Thank you very much.


Top
 Profile  
 
 Post subject: Re: Hibernate trying to create table instead of insert
PostPosted: Sun Sep 13, 2009 10:35 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Really, you just gotta keep it simple. I think that tutorial is a little much for an intro. Plus, I've seen others have similar problems with it.

I use annotations, but keep things real simple, something like this:

Code:
@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!!!"); 
  }
}


Notice the session.beginTransaction(); and the session.getTransaction().commit(); That's really what you usually need if you want to interact with a database.

Here are some real easy to follow tutorials:

http://jpa.ezhibernate.com/Javacode/lea ... hhibernate

_________________
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  
 
 Post subject: Re: Hibernate trying to create table instead of insert
PostPosted: Sun Sep 13, 2009 2:30 pm 
Newbie

Joined: Sat Sep 12, 2009 11:21 am
Posts: 5
It does look simpler than what i was trying to do.

I added Annotations libraries in my classpath and changed my code to this:

Code:
package acessoBanco;

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;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@Entity
public class Leitor {

     private long id;
     private int posX;
     private int posY;
     private int posZ;
     private String nome;
    
     @Id
     @GeneratedValue
     public long getId(){ return id;}
    
     public void setId(long id){this.id = id;}
    
     public String getNome(){ return nome;}
    
     public void setNome(String nome){this.nome = nome;}    
    
     public int getPosX(){ return posX;}
    
     public void setPosX(int posX){ this.posX = posX;}
    
     public int getPosY(){ return posY;}
    
     public void setPosY(int posY){ this.posY = posY;}
    
     public int getPosZ(){ return posZ;}
    
     public void setPosZ(int posZ){ this.posZ = posZ;}
    
     public static void main(String args[]){
          AnnotationConfiguration config = new AnnotationConfiguration();
          config.addAnnotatedClass(Leitor.class);
          config.configure();
          // new SchemaExport(config).create(true, true);
          SessionFactory factory = config.buildSessionFactory();
          Session session = factory.getCurrentSession();
          session.beginTransaction();
          System.out.println("creating leitor");
         
          Leitor u = new Leitor();
          u.setNome("Leitor 7");
          u.setPosX(0);
          u.setPosY(0);

          session.saveOrUpdate(u);
          System.out.println("leitor saved");
          session.getTransaction().commit();
          System.out.println("transaction successful!!!"); 
        }
}


But I'm getting this error:

Code:
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
   at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:572)
   at acessoBanco.Leitor.main(Leitor.java:50)


But if i change it to Session session = factory.openSession(); instead of Session session = factory.GetCurrentSession(); it works.

I also tried to call two methods in main, one opening a session and the other getting it but I got the same error.
What does it mean? Is there another thing I have to do before using GetCurrentSession?

Thank you again for your kindness.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.