Hej,
mam problem z Java - konkretnie hibernate. Nie jestem w stanie wyciagnac rekordow z BD i wyprowadzic ich na konsole, o jakies zwyklej tabeli nie wspominajac. Mozecie zerknac na kod?
Ogolnie dostaje Exception "rezerwacja is not mapped" (Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: rezerwacja is not mapped [from rezerwacja]).
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: rezerwacja is not mapped [from rezerwacja]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:327)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3325)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:733)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:256)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
at com.kamil.aktywujRezerwacje.PokazRezerwacje.main(PokazRezerwacje.java:17)
/**
*
*/
package com.kamil.rezerwacja;
import java.util.Date;
import javax.persistence.*;
//import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.kamil.rezerwacja.HibernateUtil;
@Entity
@Table
public class Rezerwacja {
private static final String SUCCESS = "success";
private static final String ERROR = "error";
private int idRezerwacji;
private int nrPokoju;
private String imie;
private String nazwisko;
private String firma;
private String adresEMail;
private String nrTelefonu;
private String notatka;
private Date dataPrzyjazdu;
private Date dataOdjazdu;
private String idKlienta;
private String idPracownika;
@Id
@GeneratedValue
public int getIdRezerwacji() {
return idRezerwacji;
}
public void setIdRezerwacji(int idRezerwacji) {
this.idRezerwacji = idRezerwacji;
}
public int getNrPokoju() {
return nrPokoju;
}
public void setNrPokoju(int nrPokoju) {
this.nrPokoju = nrPokoju;
}
public String getImie() {
return imie;
}
public void setImie(String imie) {
this.imie = imie;
}
public String getNazwisko() {
return nazwisko;
}
public void setNazwisko(String nazwisko) {
this.nazwisko = nazwisko;
}
public String getFirma() {
return firma;
}
public void setFirma(String firma) {
this.firma = firma;
}
public String getAdresEMail() {
return adresEMail;
}
public void setAdresEMail(String adresEMail) {
this.adresEMail = adresEMail;
}
public String getNrTelefonu() {
return nrTelefonu;
}
public void setNrTelefonu(String nrTelefonu) {
this.nrTelefonu = nrTelefonu;
}
public String getNotatka() {
return notatka;
}
public void setNotatka(String notatka) {
this.notatka = notatka;
}
public String getIdKlienta() {
return idKlienta;
}
public void setIdKlienta(String idKlienta) {
this.idKlienta = idKlienta;
}
public String getIdPracownika() {
return idPracownika;
}
public void setIdPracownika(String idPracownika) {
this.idPracownika = idPracownika;
}
public Date getDataPrzyjazdu() {
return dataPrzyjazdu;
}
public void setDataPrzyjazdu(Date dataPrzyjazdu) {
this.dataPrzyjazdu = dataPrzyjazdu;
}
public Date getDataOdjazdu() {
return dataOdjazdu;
}
public void setDataOdjazdu(Date dataOdjazdu) {
this.dataOdjazdu = dataOdjazdu;
}
public String save (){
String result = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Rezerwacja rez = new Rezerwacja();
rez.setNrPokoju(this.getNrPokoju());
rez.setImie(this.getImie());
rez.setNazwisko(this.getNazwisko());
rez.setAdresEMail(this.getAdresEMail());
rez.setNrTelefonu(this.getNrTelefonu());
rez.setNotatka(this.getNotatka());
rez.setDataPrzyjazdu(this.getDataPrzyjazdu());
rez.setDataOdjazdu(this.getDataOdjazdu());
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(rez);
tx.commit();
result = SUCCESS;
} catch (Exception e) {
if (tx != null) {
tx.rollback();
result = ERROR;
e.printStackTrace();
}
} finally {
session.close();
}
return result;
}
}
package com.kamil.rezerwacja;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
public static SessionFactory getSessionFactory() {
if(sessionFactory == null) {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
public static void setSessionFactory(SessionFactory sessionFactory) {
HibernateUtil.sessionFactory = sessionFactory;
}
}
Hibernate.cfg.xml
<?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.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Projekt_2</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">qwe</property>
<!-- Enalble hibernate current session context -->
<property name="current_session_context_class">thread</property>
<!-- SQL Dialekt -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- JDBC connection pool size -->
<property name="connection.pool_size">2</property>
<!-- Echo all executed SQLs -->
<property name="show_sql">true</property>
<property name="hbm2dll.auto">update</property>
<!-- Ech all executed SQLs to stdout -->
<property name="show_sql">true</property>
<!-- Disable the second level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping package="com.kamil.hotel" />
<mapping class="com.kamil.rezerwacja.Rezerwacja" />
<!-- <mapping resource="hibernate.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>
Powzszy kod dziala, jezeli chodzi o insert. Dla selektu mam ponizsza klase:
package com.kamil.aktywujRezerwacje;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.kamil.rezerwacja.HibernateUtil;
import com.kamil.rezerwacja.Rezerwacja;
public class PokazRezerwacje {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Query q = session.createQuery("from rezerwacja");
List <Rezerwacja> l1 = q.list();
for (Rezerwacja l2:l1 ){
System.out.println(l2.getIdRezerwacji());
}
W zwiazku z powyzszym bledem stworzylem hibernate.hbm.xml. Kiedy usuwam wykomentowanie i dolaczam plik do kodu dostaje:
Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: com.kamil.rezerwacja.Rezerwacja column: nrtelefonu (should be mapped with insert="false" update="false")
Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: com.kamil.rezerwacja.Rezerwacja column: nrtelefonu (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
at com.kamil.rezerwacja.HibernateUtil.getSessionFactory(HibernateUtil.java:12)
at com.kamil.aktywujRezerwacje.PokazRezerwacje.main(PokazRezerwacje.java:16)
<?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 name="com.kamil.rezerwacja.Rezerwacja" table="rezerwacja">
<id name="idRezerwacji" column="idrezerwacji" type="integer">
<generator class="assigned" />
</id>
<property name="nrPokoju" column="nrpokoju" type="integer" />
<property name="imie" column="imie" type="string" />
<property name="nazwisko" column="nazwisko" type="string" />
<property name="firma" column="firma" type="string" />
<property name="adresEMail" column="adresemail" type="string" />
<property name="nrTelefonu" column="nrtelefonu" type="string" />
<property name="notatka" column="nrtelefonu" type="string" />
<property name="dataPrzyjazdu" column="dataprzyjazdu" type="date" />
<property name="dataOdjazdu" column="dataodjazdu" type="date" />
<property name="idKlienta" column="idklienta" type="string" />
<property name="idPracownika" column="idpracownika" type="string" />
</class>
</hibernate-mapping>
Ktos jest w stanie ogarnac, co jest nie tak?