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>
|