Hibernate - errors

Odpowiedz Nowy wątek
Hibernater
2012-08-07 15:24
Hibernater
0

Właśnie zacząłem naukę Hibernate i już na początku mam problemy. Oto moje pliki:

///////////////////////////////////////////////////////////////////////////////////

package roseindia;

public class Person {
    int id;
    String name;

    public Person() {
    }

    public Person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

///////////////////////////////////////////////////////////////////////////////////

package roseindia;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class PersonDetail {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    public static void main(String[] args) {
        Session session = null;
        try {
            try {
                Configuration cfg = new Configuration().addResource(
                        "person.hbm.xml").configure();
                serviceRegistry = new ServiceRegistryBuilder().applySettings(
                        cfg.getProperties()).buildServiceRegistry();
                sessionFactory = cfg.buildSessionFactory(serviceRegistry);
            } catch (Throwable ex) {
                System.err.println("Failed to create sessionFactory object."
                        + ex);
                throw new ExceptionInInitializerError(ex);
            }
            session = sessionFactory.openSession();

            Person person = new Person();
            System.out.println("Inserting Record");
            Transaction tx = session.beginTransaction();
            person.setId(1);
            person.setName("Roseindia");
            session.save(person);
            tx.commit();
            System.out.println("Done");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            session.close();
        }
    }
}

///////////////////////////////////////////////////////////////////////////////////

<code class="xml"><?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="roseindia">
<class name="Person" table="person">
<id name="id" type="int" column="Id" >
<generator class="assigned"/>
</id>

<property name="name">
<column name="Name" />
</property>

</class>
</hibernate-mapping>

///////////////////////////////////////////////////////////////////////////////////

<!DOCTYPE hibernate-configuration PUBLIC
"-Hibernate/Hibernate Configuration DTD 3.0EN"
"http://hibernate.sourceforge.[...]guration-3.0.dtd"&gt;

<hibernate-configuration> <session-factory> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:mem:testdb</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="connection.pool_size">2</property> <property name="dialect">org.hibernate.dialect.HSQLDialect</property> <property name="show_sql">true</property> <mapping resource="person.hbm.xml" /> </session-factory> </hibernate-configuration>

///////////////////////////////////////////////////////////////////////////////////

Błąd: 
```java
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Session cannot be resolved to a type
    Configuration cannot be resolved to a type
    Configuration cannot be resolved to a type
    ServiceRegistry cannot be resolved to a type
    ServiceRegistryBuilder cannot be resolved to a type
    SessionFactory cannot be resolved to a type
    ServiceRegistry cannot be resolved to a type
    SessionFactory cannot be resolved to a type
    Transaction cannot be resolved to a type

    at roseindia.PersonDetail.main(PersonDetail.java:17)

///////////////////////////////////////////////////////////////////////////////////

Co jest źle ?

Pozostało 580 znaków

2012-08-07 15:30
Moderator

Rejestracja: 16 lat temu

Ostatnio: 1 godzina temu

0

Build path jest zły? Tzn brakuje tam Hibernata na oko...


Masz problem? Pisz na forum, nie do mnie. Nie masz problemów? Kup komputer...

Pozostało 580 znaków

Hibernater
2012-08-07 16:12
Hibernater
0

No coś tam namieszałem, ale poprawiłem i znów jest źle:

sie 07, 2012 4:09:54 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
sie 07, 2012 4:09:54 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.5.SP1}
sie 07, 2012 4:09:54 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
sie 07, 2012 4:09:54 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
sie 07, 2012 4:09:54 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: person.hbm.xml
sie 07, 2012 4:09:54 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
sie 07, 2012 4:09:54 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
sie 07, 2012 4:09:59 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: person.hbm.xml
sie 07, 2012 4:09:59 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
sie 07, 2012 4:09:59 PM org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: roseindia.Person -> roseindia.Person
sie 07, 2012 4:09:59 PM org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: roseindia.Person -> Person
Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Could not parse mapping document from resource person.hbm.xml
Exception in thread "main" java.lang.NullPointerException
    at roseindia.PersonDetail.main(PersonDetail.java:44)

Pozostało 580 znaków

2012-08-07 19:37
Moderator

Rejestracja: 16 lat temu

Ostatnio: 1 godzina temu

0

Ale czego nie rozumiesz? Masz napisane że Hibernate ma problem z mappingiem PersonDetail.


Masz problem? Pisz na forum, nie do mnie. Nie masz problemów? Kup komputer...

Pozostało 580 znaków

Hibernater
2012-08-07 20:58
Hibernater
0

no tak, ale nie mam pojęcia jak to naprawić. Mógłbym prosić o pomoc ?

Pozostało 580 znaków

2012-08-07 21:44

Rejestracja: 9 lat temu

Ostatnio: 6 lat temu

0

Tu przypadkiem nie powinna być ścieżka do config'a hibernate?

 Configuration cfg = new Configuration().addResource("person.hbm.xml").configure();

Idiots, idiots everywhere.

Pozostało 580 znaków

Odpowiedz

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0