Hi Everyone,
I'm a newbie and have been struggling with this issue for quite sometime: I 've created a postgres database and have added a few rows to a table called "CLIENT_MASTER" , however, when I try to list the rows using code, i dont get anything in the list. The following is the
client method im using :
Code:
private static void listClient() {
Transaction tx = null;
//Session session = SessionFactoryUtil.getInstance().getCurrentSession();
SessionFactory fact = new Configuration().configure().buildSessionFactory();
Session session = fact.openSession();
try {
tx = session.beginTransaction();
Query query=session.createQuery("select h from com.Category h");
List<Client_Master> clientList = session.createQuery("select h from "+Client_Master.class.getName()+" h").list();
System.out.println("???????? "+clientList.size());
for (Iterator iter = clientList.iterator(); iter.hasNext();) {
Client_Master element = (Client_Master) iter.next();
// logger.debug("{}", element);
System.out.println(">>>>>>>> "+element.getGeography());
}
tx.commit();
} catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (Exception e1) {
logger.debug("Error rolling back transaction");
}
// throw again the first exception
throw e;
}
}
}
Above, clientList.size() returns 0, thats when there are 2 rows in the CLIENT_MASTER table.
The following is
Client_Master.java bean :
Code:
package com;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CLIENT_MASTER")
public class Client_Master implements Serializable {
@Id
@Column(name="product_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long product_id;
@Column(name="sub_category_id")
private Long sub_category_id;
@Column(name="product_name")
private String product_name;
@Column(name="product_website")
private String product_website;
@Column(name="product_company")
private String product_company;
@Column(name="geography")
private String geography;
@Column(name="timestamp")
private String timestamp;
@Column(name="cumulative_rating")
private Long cumulative_rating;
public Long getProduct_id() {
return product_id;
}
public void setProduct_id(Long product_id) {
this.product_id = product_id;
}
public Long getSub_category_id() {
return sub_category_id;
}
public void setSub_category_id(Long sub_category_id) {
this.sub_category_id = sub_category_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getProduct_website() {
return product_website;
}
public void setProduct_website(String product_website) {
this.product_website = product_website;
}
public String getProduct_company() {
return product_company;
}
public void setProduct_company(String product_company) {
this.product_company = product_company;
}
public String getGeography() {
return geography;
}
public void setGeography(String geography) {
this.geography = geography;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public Long getCumulative_rating() {
return cumulative_rating;
}
public void setCumulative_rating(Long cumulative_rating) {
this.cumulative_rating = cumulative_rating;
}
}
The following is my
persistence.xml :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="HibernateMapping">
<class>com.Client_Master</class>
<class>com.Category</class>
<!-- <class>com.Search_Tolerence</class>
<class>com.Web_Source_Master</class>
<class>com.Web_Source_Result</class> -->
<properties>
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" />
</properties>
</persistence-unit>
</persistence>
The following is my
hibernate.cfg.xml :
Code:
<?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="connection.datasource">HibernateMapping</property>
-->
<property name="connection.url">
jdbc:postgresql://localhost/mediaAnalytics
</property>
<property name="connection.username">postgres</property>
<property name="connection.driver_class">
org.postgresql.Driver
</property>
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<property name="connection.password">admin@123</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property><!-- thread is the short name for org.hibernate.context.ThreadLocalSessionContext and let Hibernate bind the session automatically to the thread -->
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
<!--
<mapping class="com.Client_Master" />
-->
<!-- mapping files
<mapping resource="de/laliluna/example/Honey.hbm.xml" />
-->
</session-factory>
</hibernate-configuration>
Please help!!
Thanks,
Ravissant Markenday