I'm learning to use hibernate, and I do not know if it's bad luck but every tutorial I find and implement ends up giving me exceptions, even if they are official tutorials.
My problem is that when creating a session and saving an object of type User in its respective postgresql table it gives me the following exception: java.lang.reflect.InvocationTargetException
This is the stacktrace:
ott 17, 2018 3:51:33 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.6.Final}
ott 17, 2018 3:51:33 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
ott 17, 2018 3:51:35 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
ott 17, 2018 3:51:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
ott 17, 2018 3:51:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/User_Configuration]
ott 17, 2018 3:51:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=postgres, password=****}
ott 17, 2018 3:51:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
ott 17, 2018 3:51:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
ott 17, 2018 3:51:36 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
ott 17, 2018 3:51:36 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
ott 17, 2018 3:51:36 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@4004bfba
The class for the creation of Session's:
package Controller;
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory sessionFactory;
static{
sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
And a test class where I try to save a User (the user class is generated through the cross-engineering tool of hibernate):
package Controller;
import org.hibernate.Session;
import org.hibernate.Transaction;
import Tables.User;
public class Test {
private static Session session;
public static void main(String[] args) {
session = HibernateUtils.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
User user1 = new User("1","Bryan","Romero","123","mycode");
tx.commit();
session.close();
System.exit(0);
}
}
The User class:
package Tables;
// Generated 17-ott-2018 15.24.27 by Hibernate Tools 5.1.0.Alpha1
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* User generated by hbm2java
*/
@Entity
@Table(name = "user", schema = "public")
public class User implements java.io.Serializable {
@Id
@Column(name="userid")
private String userid;
@Column(name="name")
private String name;
@Column(name="surname")
private String surname;
@Column(name="password")
private String password;
@Column(name="codice_fiscale")
private String codiceFiscale;
public User() {
}
public User(String userid) {
this.userid = userid;
}
public User(String userid, String name, String surname, String password, String codiceFiscale) {
this.userid = userid;
this.name = name;
this.surname = surname;
this.password = password;
this.codiceFiscale = codiceFiscale;
}
public String getUserid() {
return this.userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return this.surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCodiceFiscale() {
return this.codiceFiscale;
}
public void setCodiceFiscale(String codiceFiscale) {
this.codiceFiscale = codiceFiscale;
}
}