how can I update a table in hibernate? - java

I am going to start hibernate so I download a dummy code from net my problem is wherever i am running this code each time its creating a new table my data is not updating in current table please help me..
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.javatpoint.mypackage.Employee" table="emp1000">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="firstName"></property>
<property name="lastName"></property>
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>

your hibernate xml is configure to create a new relation each time.
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
</session-factory>

Related

Warning shown when creating hibernate.cfg.xml in Eclipse

Here is my code in file named 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.url">jdbc:mysql://localhost:3306/HeartJar</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/heartjar/bean/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
There was a waining saying Stream not availableat line 2
Although warnings don't have bad influences to my project.But Understanding how to solve the problem is useful for me,a beginner in JavaWeb.Thank you for your answer

Hibernate: InvalidMappingException

I'm using hibernate framework for a desktop application (Swing) and I called the needed librairies for hibernte to work but I still get this exception when I create a SessionFactory:
private static SessionFactory factory = new Configuration(). configure(). buildSessionFactory();
And this is the list of used librairies:
My config 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.PostgreSQLDialect
</property>
<property name="hibernate.connection.driver_class">
org.postgresql.Driver
</property>
<property name="hibernate.connection.url">
jdbc:postgresql://localhost:5432/test_hibernate
</property>
<property name="hibernate.connection.username">
postgres
</property>
<property name="hibernate.connection.password">
root
</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My 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="Employee" table="employee">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="salary" column="salary" type="double"/>
</class>
</hibernate-mapping>
Try removing encoding part from top level tag in both config and hbm file.
I am not sure if this will work for you, it worked for us.

Hibernate, Could not parse configuration

I am new to Hibernate .... i tried this code but i got Could not parse configuration: NewFile.cfg.xml (name of .cfg.xml 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>
<!-- connection to oracle -->
<propertyname="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:XE
</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>
<!-- autocommit -->
<property name="hibernate.connection.autocommit">false</property>
<!-- to display sql query generated while running program -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!-- for jdbc transaction -->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.
JDBCTransactionFactory</property>
<!-- mapping file -->
<mapping resource="Mapping.hbm.xml"/>
</session-factory>
The end tag for <hibernate-configuration> is missing:
</hibernate-configuration>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
Your opening tag not match the ending tag.

Hibernate mapping when cfg file and entity file are in different folders

I've got project with this structure:
When I'm trying to access dtb via hibernate, I get this exception:
Initial SessionFactory creation failed.org.hibernate.MappingException: entity class not found: user/DBUser
V 02, 2013 9:17:10 ODP. org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/fit] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
java.lang.ClassNotFoundException: user/DBUser
Could you please show me, how should the path in my config files look like? I've tried several combinations, but I can't figure out, how to write it.
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="DontKnowWhatShallBeHere/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>
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="DontKnowWhatShallBeHere/DBUser.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
Everything looks alright. Can you try this and see :
<class name="user.DBUser" table="DBUSER">

Issue with Eclipse Hibernate Tools

I'm trying to use the Hibernate Code Generation feature in Hibernate Tools Eclipse Add-On. It is giving me the following error:
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Alert.hbm.xml
Could not parse mapping document from resource Alert.hbm.xml
org.hibernate.MappingException: class Alert not found while looking for property: alertId
class Alert not found while looking for property: alertId
org.hibernate.MappingException: class Alert not found while looking for property: alertId
class Alert not found while looking for property: alertId
java.lang.ClassNotFoundException: Alert
Alert
It is not finding the class Alert.java but I thought the Code Generator (hence the name...) was supposed to generate all the hibernate classes for me.
Using eclipse Indigo with Hibernate Tools 3.4.x.
Here's my hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/findata?tcpKeepAlive=true
</property>
<property name="connection.username">root</property>
<property name="connection.password">madmax1.</property>
<property name="connection.pool_size">2</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.mysqldialect
</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<mapping resource="Alert.hbm.xml" />
<mapping resource="Entity.hbm.xml" />
<mapping resource="FactData.hbm.xml" />
<mapping resource="TimeEvent.hbm.xml" />
<mapping resource="User.hbm.xml" />
<mapping resource="AlertTarget.hbm.xml" />
<mapping resource="LogAlert.hbm.xml" />
</session-factory>
</hibernate-configuration>
Here's Alert.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="Alert" table="alerts">
<id name="alertId" column="id">
<generator class="assigned"/>
</id>
<property name="limitValue" column="limit_value" type="decimal" />
<!-- The unique=true property makes the many-to-one a one-to-one relationship -->
<many-to-one name="alertEntity"
class="Entity" column="entity_id"
not-null="true" cascade="all" unique="true"/>
<set name="alert_targets" table="alerts_alert_targets" cascade="all">
<key column="alert_id" />
<many-to-many column="alert_target_id"
class="AlertTarget" />
</set>
</class>
</hibernate-mapping>
weird it is looking for the to-be-generated class.
I will check the hibernate-reverse.xml file and check if it does not have additional attributes that may result in this.
Alternatively, during generation, try to setup the hibernate-revenge.xml and hibernate.cfg.xml rather than using the existing ones.
The cause turned out to be the fact that the "type" was not specified for the "id" property in Alert.hbm.xml.

Categories

Resources