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.0*EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <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> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">create</property>-->
<mapping resource="person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
```
///////////////////////////////////////////////////////////////////////////////////
Błąd:
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 ?