I have some problems with hibernate. I am sharing my code.
The error I am getting
"C:\Program Files\Java\jdk1.8.0_161\bin\java"
-Dfile.encoding=windows-1250 -jar C:\Users\kubci\IdeaProjects\FinalProject\target\hibernate-1.0-SNAPSHOT-jar-with-dependencies.jar
maj 29, 2018 7:41:56 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {[WORKING]}
maj 29, 2018 7:41:56 PM org.hibernate.cfg.Environment
INFO: HHH000206: hibernate.properties not found
maj 29, 2018 7:41:57 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
INFO: HCANN000001: Hibernate Commons Annotations {5.0.3.Final}
maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/loginpanel]
maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator
INFO: HHH10001003: Autocommit mode: false
maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
maj 29, 2018 7:41:58 PM org.hibernate.dialect.Dialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Exception in thread "main" org.hibernate.boot.registry.classloading.spi.ClassLoadingException:
Unable to load class [pl.Final.Project.entities.LoginConnection]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:354)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.(AnnotationMetadataSourceProcessorImpl.java:105)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.(MetadataBuildingProcess.java:156)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:150)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at pl.Final.Project.Main.main(Main.java:19)
Caused by: java.lang.ClassNotFoundException: Could not load requested class : pl.Final.Project.entities.LoginConnection
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:342)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:351)
... 9 more
This is my Main .
package pl.Final.Project;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import pl.Final.Project.entities.LoginUser;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
private static SessionFactory factory;
public static void main(String[] args) {
factory = new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
System.exit(0);
}
public static ArrayList<LoginUser> getUsersList(){
Session session = factory.openSession();
Transaction tx = null;
ArrayList<LoginUser> usersArrayList= new ArrayList<>();
try{
tx = session.beginTransaction();
List customerList = session.createQuery("FROM pl.Final.Project.entities.LoginUser WHERE id=1").list();
for(Iterator iterator = customerList.iterator(); iterator.hasNext();){
LoginUser c =(LoginUser) iterator.next();
usersArrayList.add(c);
}
tx.commit();
}catch (HibernateException e){
if(tx!=null){
tx.rollback();
}
}finally {
session.close();
}
return usersArrayList;
}
}
and cfg.xml and hmb.xml files
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/loginpanel</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="pl.Final.Project.entities.LoginConnection" />
</session-factory>
</hibernate-configuration>
<?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>
<class name = "pl.Final.Project.entities.LoginUser" table = "tuser">
<meta attribute = "class-description">
Login Customer mapping
</meta>
<id name = "id" type = "int" column = "id">
<generator class="native"/>
</id>
<property name = "login" column = "login" type = "string"/>
<property name = "password" column = "password" type = "string"/>
<property name = "name" column = "name" type = "string"/>
<property name = "surname" column = "surname" type = "string"/>
</class>
</hibernate-mapping>
I don't know what to do. I had another project with this code and it works. I did copy and paste, change some details like path etc. There are some errors and I think here is the problem:
Exception in thread "main"
org.hibernate.boot.registry.classloading.spi.ClassLoadingException:
Unable to load class [pl.Final.Project.entities.LoginConnection]
The exception you encountered says clearly that LoginConnection class can't be loaded. Yet there is no LoginConnection class to be seen. You've added LoginConnection as mapping class for Hibernate session, but source code of LoginConnection is missing. Maybe you should create LoginConnection class first, before it can be loaded?
Or did you accidently write LoginConnection instead of LoginUser?
Related
Error:
May 27, 2017 2:39:48 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
May 27, 2017 2:39:48 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 27, 2017 2:39:48 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
May 27, 2017 2:39:49 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3305/db]
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
May 27, 2017 2:39:52 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
May 27, 2017 2:39:52 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
May 27, 2017 2:39:54 PM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1]
javax.persistence.OptimisticLockException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
Code:
pojo class
public class Testpojo {
private int number;
private String name;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
main class
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestMain {
/**
* #param args
*/
public static void main(String[] args) {
Testpojo tp=new Testpojo();
tp.setNumber(3);
tp.setName("MEHUL1");
try{
SessionFactory ses=new Configuration().configure().buildSessionFactory();
Session s1=ses.openSession();
Transaction tr=s1.beginTransaction();
s1.update(tp);
tr.commit();
}catch(Exception e){
System.out.println(e);
}
}
}
Configuration file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3305/db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping resource="Testpojo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
mapping file
<?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>
<class name="Testpojo" table="t">
<id name="number" column="num">
<generator class="native"></generator>
</id>`enter code here`
<property name="name" column="name"></property>
</class>
</hibernate-mapping>
create one simple appliaction of hibernate with Testpojo.java ,Testpojo.hbm.xml,Testmain.java and hibernatecfg.xml.
how to solve "javax.persistence.OptimisticLockException:" in hibernate ?
add all jar file of hibernate but still found this error.javax.persistence.OptimisticLockException:
You got OptimisticLockException means some other transcation is already using that particular record.
im noobie here but i have been finding the fix of my problem here i dont see it... Im new with hibernate and im using my free time to learn this.
package negocio;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import integracion.HibernateUtil;
import integracion.dao.BodegaDAOImpl;
import integracion.dao.VinoDAO;
import integracion.dao.VinoDAOImpl;
import integracion.entidades.Bodega;
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory;
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
//The problem is here
**sessionFactory = configuration.buildSessionFactory(serviceRegistry);**
Session session = sessionFactory.openSession();
BodegaDAOImpl bodegaDao = new BodegaDAOImpl<>();
bodegaDao.get(1);
}
}
This is the list of the jar added1
And this is the mapping file:
<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/vinoteca</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping class="integracion.entidades.Vino"/>
<mapping class="integracion.entidades.Bodega"/>
<mapping class="integracion.entidades.Denominacion"/>
</session-factory>
</hibernate-configuration>
dic 15, 2016 12:37:51 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.9.Final}
dic 15, 2016 12:37:51 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
dic 15, 2016 12:37:51 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
dic 15, 2016 12:37:51 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/vinoteca]
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Thu Dec 15 00:37:51 CET 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
dic 15, 2016 12:37:51 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery
at org.hibernate.cfg.AnnotationBinder.bindDefaults(AnnotationBinder.java:259)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.prepare(AnnotationMetadataSourceProcessorImpl.java:184)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.prepare(MetadataBuildingProcess.java:156)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:253)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at negocio.Main.main(Main.java:31)
Caused by: java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 more
So.. this is the problem. I was seeing that the problem when eclipse says java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery
was because the jar (javax.persistence.jar or hibernate-jpa-2.1-api-1.0.0...jar) was not added, but i added them and i dont resolve this.
anyone could say me why is working bad? Thanks!
PD:Sorry for my english, im not Native english speaker =)
Use a build system like maven or gradle to build your project, and add a dependency on hibernate. That will almost certainly solve all of your basic dependency-related classpath problems.
Add the required jar files into the classpath.
I'm trying to make a software using a database, and after many tries i have followed a tutorial on youtube.
I'm using java 8, PostgreSQL 9.4 and Hibernate 5.2.2 (plus JBoss Tools 4.4.1).
I followed the tutorial until running the application, but then the following error message appears, saying that the application can't connect to the database :
oct. 15, 2016 2:53:35 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.2.Final}
oct. 15, 2016 2:53:35 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
oct. 15, 2016 2:53:35 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
oct. 15, 2016 2:53:35 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
oct. 15, 2016 2:53:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
oct. 15, 2016 2:53:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgres://localhost:5432/hibernatedb]
oct. 15, 2016 2:53:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=postgresql, password=****}
oct. 15, 2016 2:53:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
oct. 15, 2016 2:53:36 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:267)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:231)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:240)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at com.kg.hibernate.main.HibernateTest.main(HibernateTest.java:16)
Caused by: org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:postgres://localhost:5432/hibernatedb]
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:60)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:240)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:257)
... 14 more
HibernateTest.java
package com.kg.hibernate.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.kg.hibernate.UserDetails;
public class HibernateTest {
public static void main(String[] args){
UserDetails user= new UserDetails();
user.setUserId(1);
user.setUserName("Kévin");
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session= sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
UserDetails.java
package com.kg.hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class UserDetails {
#Id
private int userId;
private String 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;
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<!-- Database Connection settings -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">kgware</property>
<property name="hibernate.connection.url">jdbc:postgres://localhost:5432/hibernatedb</property>
<property name="hibernate.connection.username">postgresql</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.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="com.kg.hibernate.UserDetails"/>
</session-factory>
</hibernate-configuration>
I searched over many forums a solution but couldn't find any...
Thanks for your help !
Your Connection String is wrong modifty from
<property name="hibernate.connection.url">jdbc:postgres://localhost:5432/hibernatedb</property>
to
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
application wizard
SQL command prompt ---showing account locked because of repeated wrong passwords, which is working fine in application wizard.
Everything seems to be right even in log info it is showing the table is created and data is inserted on the same, but I am unable to see the table and data in database.Can anybody suggest the possible reason.
Database : Oracle 10g
Log Info
Jun 12, 2016 8:38:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Jun 12, 2016 8:38:30 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 12, 2016 8:38:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 12, 2016 8:38:30 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jun 12, 2016 8:38:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jun 12, 2016 8:38:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:#localhost:1521:XE]
Jun 12, 2016 8:38:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=system, password=****}
Jun 12, 2016 8:38:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jun 12, 2016 8:38:31 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Jun 12, 2016 8:38:32 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Hibernate: drop table UserDetails cascade constraints
Hibernate: create table UserDetails (userid number(10,0) not null, userName varchar2(255 char), primary key (userid))
Jun 12, 2016 8:38:33 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl#1c3146bc'
Hibernate: insert into UserDetails (userName, userid) values (?, ?)
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">system</property>
<property name="connection.password">diwakar</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:XE</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.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>
<mapping class="com.diwakar.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>
UserDetails.java
package com.diwakar.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class UserDetails {
#Id
private int userid;
private String 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;
}
}
HibernateTest.java
package com.diwakar;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.diwakar.dto.UserDetails;
public class HibernateTest {
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserid(1);
user.setUserName("Diwakar");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();
}
}
> I am using eclipse .I tried to do java hibernate program for the first time but got the above error.please help me to solve it.
downloaded hibernate release 4.2.5 final.zip and added jar files to user library
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.5.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
hibernate-entitymanager-4.2.5.Final.jar
hibernate-envers-4.2.5.Final.jar
hibernate-osgi-4.2.5.Final.jar
org.osgi.core-4.3.1.jar
is these jar files enough for hibernate program.
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>
<!-- Related to the connection START -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:#192.168.1.2:1521:xe</property>
<property name="connection.user">system</property>
<property name="connection.password">siddhu</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true</property>
<property name="dialet">org.hibernate.dialect.OracleDialect</property>
<property name="hbm2ddl.auto">update</property>
<!-- Related to hibernate properties END -->
<!-- Related to mapping START -->
<mapping resource="Details.hbm.xml"/>
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
Details.hbm.xml
- <hibernate-mapping>
- <class name="Details" table="stu">
- <id name="stNo" column="SNo">
<generator class="assigned" />
</id>
<property name="stName" column="SName" />
<property name="stAddress" />
</class>
</hibernate-mapping>
mainclass.java
import org.hibernate.*;
import org.hibernate.cfg.*;
public class mainclass {
public static void main(String[] args)
{
Configuration c = new Configuration();
c.configure("hibernate.cfg.xml");
SessionFactory factory = c.buildSessionFactory();
Session session = factory.openSession();
Details p=new Details();
p.setStno(101);
p.setStName ("iPhone");
p.setStAddress("dfdsgdf");
Transaction tx = session.beginTransaction();
session.save(p);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
}
Details.java
public class Details {
private int stNo;
private String stName;
private String stAddress;
public void setStno(int stNo)
{
this.stNo=stNo;
}
public int getStNo()
{
return stNo;
}
public void setStName(String stName)
{
this.stName=stName;
}
public String getStName()
{
return stName;
}
public void setStAddress(String stAddress)
{
this.stAddress=stAddress;
}
public String getStAddress()
{
return stAddress;
}
}
Console Error
===========================================
Sep 20, 2013 6:49:30 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Sep 20, 2013 6:49:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Sep 20, 2013 6:49:31 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Sep 20, 2013 6:49:31 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Details.hbm.xml
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:488)
at org.hibernate.cfg.Configuration.add(Configuration.java:484)
at org.hibernate.cfg.Configuration.add(Configuration.java:657)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2160)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2140)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2093)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
at mainclass.main(mainclass.java:9)
Caused by: org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 10 more
Your XML file, for whatever reason, might contain a BOM as described in this answer and its linked article.
I suggest you create a new file and write (copy) over the configuration from your mapping file.