If it is duplicate, my apology. I could not find an answer for my question.
I just start Hibernate. I have an issue is every time the program automatically delete the old shceme and creates new when it is running. For example, If I want to add records into database, I could not do it because the scheme will be recreate, so the histories will be deleted.
Here is my hbm.xml mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22-Oct-2013 1:39:31 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="net.ys.hibernate.Equip" table="EQUIP">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="dis" type="java.lang.String" column="dis" />
<property name="ref" type="java.lang.String" column="ref" />
<property name="type" type="java.lang.String" column="type" />
</class>
</hibernate-mapping>
hibernate.cfg.xml configuration file.
<?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>
<!-- hibernate dialect -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">pw</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/explorer_DB?useUnicode=true&characterEncoding=GBK</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.default_schema">explorer_DB</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Automatic schema creation(begin) -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Simple memory-only cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- mapping files with external dependencies -->
<mapping resource="net/ys/hibernate/Equip.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And addEquip method:
public Integer addEquip(String dis, String ref, String type){
Session session = sessionFactory.openSession();
currentSession = sessionFactory.getCurrentSession();
Transaction tx = null;
Integer equipID = null;
try {
tx = currentSession.beginTransaction(); //start a transaction
Equip equip = new Equip(dis,ref,type);
equipID = (Integer)currentSession.save(equip);
tx.commit();
} catch (HibernateException e) {
if(tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return equipID;
}
}
could someone help me to solve this issue? I just don't understand how to use getCurrentSession(), probably I am wrong in this point. Could you explain how hibernate works when we call getCurrentSession() for me? I really appreciate it.
Thank you so much
Change this property in your hibernate.cfg.xml file
current configuration
<property name="hibernate.hbm2ddl.auto">create</property>
to
<property name="hibernate.hbm2ddl.auto">none</property>
if no change required in database use.
<property name="hibernate.hbm2ddl.auto">validate</property>
More on this please refer this link
Hibernate hbm2ddl.auto possible values and what they do?
Related
I have a maven project that I am using with hibernate-core 4.2.3.Final. I have followed the advice provided here:
Where to place hibernate config?
Hibernate Tutorial - Where to put Mapping File?
and the config file in src/main/resources looks like this:
<?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="connection.url">jdbc:mysql://localhost:3306/justHibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">12345</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="com/mycompany/app/model/Movie.hbm.xml" />
</session-factory>
</hibernate-configuration>
while the mapping file is within the package com.mycompany.app.model alongside the Movie class:
<?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="com.mycompany.app.model.Movie" table="Movies">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="title" column="title"/>
<property name="director" column="director"/>
<property name="synopsis" column="synopsis"/>
</class>
</hibernate-mapping>
Exception
The configuration cannot find the mapping file.
INFO: HHH000221: Reading mappings from resource: com/mycompany/app/model/Movie.hbm.xml
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com/mycompany/app/model/Movie.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:738)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2167)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2139)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2119)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2072)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1966)
at com.mycompany.app.MovieManager.initSessionFactory(MovieManager.java:22)
at com.mycompany.app.MovieManager.<init>(MovieManager.java:17)
at com.mycompany.app.App.main(App.java:27)
Place Movie.hbm.xml under src/main/resources and change path in your config file as follows,
<mapping resource="Movie.hbm.xml" />
Hope this helps.
Good Morning to everyone!
I´m trying to use Hibernate to read an MSAccess database in Java using Eclipse, but it gives me a MappingException
here is my hibernateaccess.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<session-factory>
<property name='connection.driver_class'>net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name='connection.username'></property>
<property name='connection.password'></property>
<!-- JDBC connection pool (use the built-in) -->
<property name='connection.pool_size'>1000</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_size">600000</property>
<!-- SQL dialect -->
<property name='dialect'>dialect.MSAccessDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name='show_sql'>true</property>
<!-- Mapping files -->
<mapping class="TransporteAccess.hbm.xml" />
</session-factory>
</hibernate-mapping>
and the TransporteAccess.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Transporte" table="Transportes">
<property name="transporte" column="TRANSPORTES" type="string"></property>
</class>
</hibernate-mapping>
Which I´m doing wrong???
Thanks a lot!
It is mandatory to have Primary Key in Hibernate, refer
Add Primary key as well,
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
apart from this the hibernateaccess.xml contains some irrelevant tags
Update top lines in hibernateaccess.xml with
<?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>
& change
<mapping class="TransporteAccess.hbm.xml" />
to
<mapping resource="TransporteAccess.hbm.xml" />
I am trying to query an access db in java 8 and i use maven. I am using net.ucanaccess.jdbc.UcanaccessDriver for my connection and below is my hibernate.cfg.xml:
<?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">
net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name="hibernate.connection.url">jdbc:ucanaccess://D:\\db\\QIDB-Access\\db.mdb</property>
<property name="hibernate.connection.username"> </property>
<property name="hibernate.connection.password"> </property>
<mapping resource="IndikatorProcessor.hbm.xml" />
</session-factory>
</hibernate-configuration>
My class with annotations looks like this:
#Entity
#Table(name = "Indikator")
public class IndikatorProcessor {
#Id
#Column(name = "QI_ID")
private int qiId;
public int getQiId() {
return qiId;
}
public void setQiId(int qiId) {
this.qiId = qiId;
}
}
In another class i create the session and write a simple query:
....
public void listQIIndikator() {
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List<?> indikators = session.createQuery("From IndikatorProcessor").list();
for (Iterator<?> iterator = indikators.iterator(); iterator.hasNext();) {
IndikatorProcessor indiaktor = (IndikatorProcessor) iterator.next();
System.out.println("Indikator ID = " + indiaktor.getQiId());
}
tx.commit();
I get this error:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: IndikatorProcessor is not mapped
I am not sure what causes the error since it is mapped! Is it the query or using the ucanaccess driver for the connection?
Now i added a IndikatorProcessor.hbm.xml as folow and changed the hibernate file to use it.
<?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="IndikatorProcessor" table="Indikator">
<meta attribute="class-description">
This class contains the Indikator detail.
</meta>
<id name="qiId" type="int" column="QI_ID"> <!-- <generator class="native"/> --> </id>
<!-- <property name="qiId" column="QI_ID" type="int"/> -->
<property name="fkQig" column= "FK_QIG"></property>
Now i get these errors:
Caused by: org.hibernate.MappingException: class IndikatorProcessor not found while looking for property: fkQig
and
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [IndikatorProcessor]
Exception says your IndikatorProcessor is not mapped in SessionFactory, so do a mapping as follows:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class"> net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name="hibernate.connection.url">jdbc:ucanaccess://D:\\db\\QIDB-Access\\db.mdb</property>
<property name="hibernate.connection.username"> </property>
<property name="hibernate.connection.password"> </property>
<mapping class="your.package.IndikatorProcessor"/>
</session-factory>
</hibernate-configuration>
hibernate could not find the class without giving the package name. I added the package name in IndikatorProcessor.hbm.xml and it worked.
<hibernate-mapping>
<class name="model.IndikatorProcessor" table="Indikator">
<meta attribute="class-description">
This class contains the Indikator detail.
</meta>
<id name="qiId" type="int" column="QI_ID"> </id>
<property name="fkQig" type ="int" column= "FK_QIG"></property>
I'm having trouble getting Hibernate to correctly reverse engineer a MySQL database into a Java Class.
I have 3 tables:
departments (pk = dept_no)
employees (pk = emp_no)
dept_emp (composite pks dept_no and emp_no)
I am trying to modify the Hibernate Reverse Engineering file to force the DeptEmp Java class that is generated to link a single department to a set of employees within that department, but I keep getting the same error, even though I have declared a name attribute for the set:
org.hibernate.MappingException: Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
org.hibernate.MappingException: Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
org.hibernate.MappingException: invalid override definition
invalid override definition
org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 88; Attribute "name" must be declared for element type "set".
Attribute "name" must be declared for element type "set".
My Reverse Engineering File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-catalog="employees" match-name="departments" />
<table-filter match-catalog="employees" match-name="employees" />
<table-filter match-catalog="employees" match-name="dept_emp" />
<table name="dept_emp" class="com.DeptEmp">
<set name="employeesSet" table="employees" inverse="true" lazy="true" fetch="select">
<key>
<column name="dept_no" ></column>
</key>
<one-to-many class="com.Employees"></one-to-many>
</set>
</table>
</hibernate-reverse-engineering>
My Hibernate Config:
<?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">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/employees</property>
<property name="hibernate.connection.username">****</property>
<property name="hibernate.connection.password">****</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.default_catalog">employees</property>
<property name="connection.pool_size">20</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
</session-factory>
</hibernate-configuration>
I'm trying to set up Hibernate 3.6 in my existing Spring 3 MVC + Spring 3 security project. Nevertheless I keep getting java.lang.ClassNotFoundException in my HibernateUtil class - line with return new Configuration().configure().buildSessionFactory();
My project structure looks like this:
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.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3049/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="./DBUser.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
DBUser.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="../src/main/java/user/DBUser" table="DBUSER">
<id name="userId" type="int">
<column name="USER_ID" precision="5" scale="0" />
<generator class="assigned" />
</id>
<property name="username" type="string">
<column name="USERNAME" length="20" not-null="true" />
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="20" not-null="true" />
</property>
<property name="createdDate" type="date">
<column name="CREATED_DATE" length="7" not-null="true" />
</property>
</class>
</hibernate-mapping>
And a relevant part of the pom.xml file:
<!-- Hibernate + MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
EDIT: I've pasted here the stack trace. It was too long to put it right here, because it would make this post confused.
I'd be really glad, if someone could help me with setting up this thing, because I've got no idea, why it can't find that class.
It looks like you are missing the javaassist library. Download the javaassist jar and add it to your classpath.
Your class name should be the package path to the Hibernate POJO in DBUser.hbm.xml, e.g:
user/DBUser
And your mapping resource does not need ./ in the path.