-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Inserting into table
PostPosted: Thu Jan 18, 2007 7:09 pm 
Newbie

Joined: Thu Jan 18, 2007 6:15 pm
Posts: 2
Hi,
I'm new to hibernate. I use struts, tomcat postgresql db in my project. I add some entities into table. But when i re-run my project, the new entities aren't appended to table.
For example, for the first time I add sth to table, it is like

ZZZ TABLE
1 aaa bbb ccc
2 ddd eee fff
3 ggg hhh iii
4 jjj kkk lll
5

But after re-running, it doesn't append the new objects although it adds into the table

ZZZ TABLE:
1 mmm nnn ooo
2

( "5 mmm nnn ooo" is expected)



My code:

...
News nw = new News();
nw.setHeader("aaa");
nw.setContent("bbb");
nw.setSource("ccc");
NewsOperations.createNews(nw);

...

public static void createNews(News nw) {
Transaction tx = null;
Session session = HibernatePlugIn.getInstance.getCurrentSession();
try {
tx = session.beginTransaction();
session.save(nw);
tx.commit();
}catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
}

...



import javax.naming.InitialContext;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

public class HibernatePlugIn {

private HibernatePlugIn() {
}

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;

public static SessionFactory getInstance() {
if (sessionFactory == null)
initSessionFactory();
return sessionFactory;
}

public Session openSession() {
return sessionFactory.getCurrentSession();
}

public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}

private static synchronized void initSessionFactory() {
Logger log = Logger.getLogger(HibernatePlugIn.class);
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
String sessionFactoryJndiName = cfg
.getProperty(Environment.SESSION_FACTORY_NAME);
if (sessionFactoryJndiName != null) {
cfg.buildSessionFactory();
log.debug("get a jndi session factory");
sessionFactory = (SessionFactory) (new InitialContext())
.lookup(sessionFactoryJndiName);
} else{
log.debug("classic factory");
sessionFactory = cfg.buildSessionFactory();
}
} catch (Exception e) {
System.err
.println("%%%% Error Creating HibernateSessionFactory %%%%");
e.printStackTrace();
throw new HibernateException(
"Could not initialize the Hibernate configuration");
}
}
}

public static void close(){
if (sessionFactory != null)
sessionFactory.close();
sessionFactory = null;

}
}


...

Hibernate.cfg.xml

<session-factory>
<!-- PostgreSQL connection -->
<property name="connection.url">jdbc:postgresql://localhost/db</property>
<property name="connection.username">postgres</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>


<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="package/News.hbm.xml" />

</session-factory>


...

News.hbm.xml

<?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 package="package">
<class name="News" table="news" >
<id name="id" >
<generator class="sequence">
<param name="sequence" >news_id_seq</param>
</generator>
</id>
<property name="header" type="string"></property>
<property name="content" type="string"></property>
<property name="source" type="string"></property>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Comment out hbm2ddl
PostPosted: Thu Jan 18, 2007 7:59 pm 
Newbie

Joined: Thu Jan 18, 2007 6:54 pm
Posts: 3
Location: Dallas
I think your problem has to do with your
<property name="hibernate.hbm2ddl.auto">create</property>
In your config file. This property tells hibernate to drop the table and rebuild every time. If you want hibernate to generate your schema the first time use this property set as you have it. Once you have your tables created in your DB comment out this property and then use your update command. I think that will fix your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 7:41 am 
Newbie

Joined: Thu Jan 18, 2007 6:15 pm
Posts: 2
Yes, it solved. Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.