Provo a postare tutto il progetto:
codice:
package org.massimo.dettagliUser;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UtenteUser {
@Id
private int userId;
private String userName;
public UtenteUser() {
// TODO Auto-generated constructor stub
}
public UtenteUser(int userId, String userName) {
super();
this.userId = userId;
this.userName = userName;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Errori di parsing:
codice:
ott 17, 2016 11:34:27 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.9.Final}
ott 17, 2016 11:34:28 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
ott 17, 2016 11:34:28 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
ott 17, 2016 11:34:28 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Exception in thread "main" org.hibernate.boot.InvalidMappingException: Could not parse mapping document: org/massimo/config/utenteuser.hbm.xml (RESOURCE)
at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:46)
at org.hibernate.boot.jaxb.internal.UrlXmlSource.doBind(UrlXmlSource.java:36)
at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:59)
at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at org.massimo.hibernate.HibernateTest.main(HibernateTest.java:26)
Caused by: org.hibernate.boot.MappingException: Unable to perform unmarshalling at line number 0 and column 0. Message: null : origin(org/massimo/config/utenteuser.hbm.xml)
at org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:177)
at org.hibernate.boot.jaxb.internal.MappingBinder.doBind(MappingBinder.java:61)
at org.hibernate.boot.jaxb.internal.AbstractBinder.doBind(AbstractBinder.java:102)
at org.hibernate.boot.jaxb.internal.AbstractBinder.bind(AbstractBinder.java:57)
at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:43)
... 9 more
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[javax.xml.stream.XMLStreamException: ParseError at [row,col]:[13,20]
Message: Le strutture di documenti XML devono iniziare e finire con la stessa entità.]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:171)
... 13 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[13,20]
Message: Le strutture di documenti XML devono iniziare e finire con la stessa entità.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at com.sun.xml.internal.stream.XMLEventReaderImpl.peek(Unknown Source)
at javax.xml.stream.util.EventReaderDelegate.peek(Unknown Source)
at org.hibernate.boot.jaxb.internal.stax.BufferedXMLEventReader.peek(BufferedXMLEventReader.java:96)
at javax.xml.stream.util.EventReaderDelegate.peek(Unknown Source)
at org.hibernate.boot.jaxb.internal.stax.HbmEventReader.peek(HbmEventReader.java:47)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleCharacters(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(Unknown Source)
... 16 more
Classe test:
codice:
package org.massimo.hibernate;
import java.util.List;
import java.util.Scanner;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.massimo.dettagliUser.UtenteUser;
public class HibernateTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
UtenteUser user = new UtenteUser();
user.setUserId(1);
user.setUserName("First user");
UtenteUser userdue = new UtenteUser();
userdue.setUserId(2);
userdue.setUserName("Second user");
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session sessione = sessionFactory.openSession();
sessione.beginTransaction();
sessione.save(user);
sessione.save(userdue);
sessione.getTransaction().commit();
System.out.println("numero id = " + user.getUserId());
System.out.println("nome user = " + user.getUserName());
System.out.println("numero id = " + userdue.getUserId());
System.out.println("nome user = " + userdue.getUserName());
System.out.println(HibernateTest.listaUtenti());
}
public static List<UtenteUser> listaUtenti() {
List<UtenteUser> result = null;
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
//Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query q = session.createQuery("from utenteuser");
result = q.list();
tx.commit();
} catch (HibernateException he) {
if (tx != null)
tx.rollback();
throw he;
} finally {
session.close();
}
return result;
}
}
File configurazione hibernate.cfg.xml:
codice:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/provahib</property>
<property name="connection_pool_size">1</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping class="org.massimo.dettagliUser.UtenteUser" />
<!-- <mapping resource="employee.hbm.xml" /> -->
<mapping resource="org/massimo/config/utenteuser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
File di risorse utenteuser.hbm.xml:
codice:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="hibernate" package="org.massimo.dettagliUser.UtenteUser">
<class name="UtenteUser" table="utenteuser" lazy="false">
<id name="id" column="userid">
<generator class="native" />
</id>
<property name="username" />
</class>
</hibernate-mapping
hib.jpg