Błędy z hibernate :-(

0

Witam,
właśnie zacząłem zabawę z Hibernate wykonuje pierwsze ćwiczenia z tutoriala ale nie mogę odpalić programu. Baza danych postgresql. Wydaje mi się ,że problem tkwi w pliku konfiguracyjnym ale nie wiem dokładnie o co może chodzić. Proszę o pomysły i wskazówki.

Oto mój plik hibernate.cfg.xml:

<!--
~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as ~ indicated by the @author tags or express copyright attribution ~ statements applied by the authors. All third-party contributions are ~ distributed under license by Red Hat Inc. ~ ~ This copyrighted material is made available to anyone wishing to use, modify, ~ copy, or redistribute it subject to the terms and conditions of the GNU ~ Lesser General Public License, as published by the Free Software Foundation. ~ ~ This program is distributed in the hope that it will be useful, ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License ~ for more details. ~ ~ You should have received a copy of the GNU Lesser General Public License ~ along with this distribution; if not, write to: ~ Free Software Foundation, Inc. ~ 51 Franklin Street, Fifth Floor ~ Boston, MA 02110-1301 USA
-->
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/moja</property>
<property name="connection.username">szymon</property>
<property name="connection.password">123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect.class</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</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>
<!-- Names the annotated entity class -->
<mapping class="Modele.Produkt"/>
</session-factory>
</hibernate-configuration>

a wywala mi takie błędy:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2241)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at StartProgram.main(StartProgram.java:21)
Caused by: org.xml.sax.SAXParseException: Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
... 3 more

ok doszedłem do tego że mam taki error
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2176)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2157)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at StartProgram.main(StartProgram.java:21)

tak wygląda teraz hibernate.cfg.xml

<!--
~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as ~ indicated by the @author tags or express copyright attribution ~ statements applied by the authors. All third-party contributions are ~ distributed under license by Red Hat Inc. ~ ~ This copyrighted material is made available to anyone wishing to use, modify, ~ copy, or redistribute it subject to the terms and conditions of the GNU ~ Lesser General Public License, as published by the Free Software Foundation. ~ ~ This program is distributed in the hope that it will be useful, ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License ~ for more details. ~ ~ You should have received a copy of the GNU Lesser General Public License ~ along with this distribution; if not, write to: ~ Free Software Foundation, Inc. ~ 51 Franklin Street, Fifth Floor ~ Boston, MA 02110-1301 USA
-->
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.6//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.6.dtd">

<hibernate-configuration>
<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/moja</property>
<property name="connection.username">szymon</property>
<property name="connection.password"></property>


<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect.class</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</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>

<!-- Names the annotated entity class -->
<mapping class="Modele.Produkt"/>

</session-factory>
</hibernate-configuration>
0

Skoro jest napisane, że nie może znaleźć pliku, to pewnie go nie ma. Zapewne masz go w złym katalogu;).

0

ok teraz już chyba jestem naprawdę blisko bo mam taki błąd:
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: Modele.Produkt not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:799)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at StartProgram.main(StartProgram.java:21)

Chyba w tej linijce jest coś nie tak ale nie wiem co:

<mapping resource="Modele.Produkt"/>

Modele to paczka a Produkt to klasa w tej paczce

0

zobacz czy ta klasa znajduje się w pakiecie Modele (musi być pełna ścieżka do niej)

0

No napisałem, że produkt to klasa w paczce Modele chyba ,że trzeba jeszcze podać nazwę projektu itd

0

na pewno nie pomyliłeś się w wielkie/małe litery? Pakiety zwykle oznacza się małymi;).

Dwa: czy czegoś nie brakuje w tym pliku? Np. jakiejś adnotacji, że ma być to tam mapowane?

0

tak wygląda klasa Produkt:

package Modele;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Produkt {
	
	@Id
	private int produktId;
	private String rodzaj;
	private String Producent;
	
	
	public int getProduktId() {
		return produktId;
	}
	public void setProduktId(int produktId) {
		this.produktId = produktId;
	}
	public String getRodzaj() {
		return rodzaj;
	}
	public void setRodzaj(String rodzaj) {
		this.rodzaj = rodzaj;
	}
	public String getProducent() {
		return Producent;
	}
	public void setProducent(String producent) {
		Producent = producent;
	}

}

i tego też nie szczajam na tym filmiku koleś w 8:10 min podaje inną lokalizacje niż ma paczkę.

0

Moge sie mylic, dawno nie konfigurowalem hb, ale z tego co pamietam, mapping resource wskazuje na pliki hbm.xml w ktorych sa mapowania klasy na tabelke itp., ty masz natomiast klase z adnotacjami - musisz to jakos inaczej skonfigurowac, cos w stylu 'mapping class="modele.Costam"'

0

Zgadza się wystarczyło zmienić tą linijkę na

<mapping class="modele.Produkt"/>
0

a weź sobie poszukaj jak skonfigurować Hibernata aby korzystał z adnotacji a nie konfiguracji przez xml -> wtedy wystarczy oznaczać encje @Entity

0

Sie obudziles - przeciez on to wlasnie robi i to byl problem.

0

Alez ty mowiles o czyms innym - anotacje @Entity to on ma, a ten xml o ktorym on mowi to hibernate.cfg.xml - zupelnie 2 rozne rzeczy. JPA rowniez wymaga xml: persistence.xml, gdzie jest kionfig polaczenia do bazy danych, jakies ustawienia, i wlasnie klasy (lub automatycznie skanowanie). W JPA nie mozna calkowicie zrezygnowac z XML, ten jeden zawsze musi byc. Nie wiem czy w HB mozna, musi sie wypowiedziec ktos z wieksza wiedza, lub trzeba poszukac. Ale ty jestes calkowicie w bledzie.

0

hibernate nie wymaga w definicji sessionFactory podawania każdej klasy z osobna któa jest mapowana

0

Ale on zrobil tak jak zrobil i ma xml. Ale ustawienia jdbc gdzies musza siedziec, wiec xml jest tak czy tak uzywany. Chyba ze robisz wszystko programistycznie. Ale ok, uznajmy, ze robi zle.

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