-->
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.  [ 1 post ] 
Author Message
 Post subject: New to hibernate, help to start with netbeans
PostPosted: Tue May 25, 2010 6:11 pm 
Newbie

Joined: Tue May 25, 2010 5:51 pm
Posts: 8
Hi all, i want use hibernate for a java swing app develops with netbeans.

My problem is the mapping of entity class, here my steps:

1- New project using Java Application
2- Create a hibernate.cfg.xml from "new file -> Hibernate -> Hibernate Configuration Wizard"
hibernate.cfg.xml is like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">org.h2.Driver</property>
    <property name="hibernate.connection.url">jdbc:h2:tcp://localhost/my_db_path</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="Hibernate/Person.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


3- Adding some props to cfg file: hibernate.hbm2ddl.auto=update and hibernate.show_slq=true
4- Create HibernateUtil.java from "new file -> Hibernate -> HibernateUtil.java"
5- Create a class that realize an object Person from "new file -> Persistance -> Entity Class"
Person.java is like this:
Code:
package Hibernate;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Person implements Serializable {
    private static final long serialVersionUID=1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    public Person() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id=id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name=name;
    }

    @Override
    public int hashCode() {
        int hash=0;
        hash+=(id!=null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if(!(object instanceof Person)) {
            return false;
        }
        Person other=(Person)object;
        if((this.id==null&&other.id!=null)||(this.id!=null&&!this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Hibernate.Person[id="+id+"]";
    }

}


5- Create Person.hbm.xml from "new file -> Hibernate -> Hibernate Mapping Wizard" and choose the Person class and NO database table (there aren't tables in db yet).
Person.hbm.xml is like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class dynamic-insert="false" dynamic-update="false" mutable="true" name="Hibernate.Person" optimistic-lock="version" polymorphism="implicit" select-before-update="false"/>
</hibernate-mapping>


I think this mapping don't work, what is my wrong step?

Im using netbeans 6.8 with hibernate plugin.
Thanks.


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

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.