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.  [ 4 posts ] 
Author Message
 Post subject: from clause is not working
PostPosted: Mon Sep 21, 2009 5:09 am 
Newbie

Joined: Mon Sep 21, 2009 4:58 am
Posts: 2
Hi to all,
i am using from clause but it is not working ,i get null value , select query works fine.

When i use from i get following message
"

Hibernate: select address0_.id as col_0_0_ from ADDRESS address0_
null
"
this is my code
plz tell me where is error

package hiber.scr;
import java.util.Iterator;
import org.hibernate.*;
import org.hibernate.cfg.*;

import java.util.*;
public class FirstExample {



public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();

// String SQL_QUERY ="Select insurance.addressId,insurance.street," +
// "insurance.city,insurance.state from Address insurance";
//Using from Clause
String SQL_QUERY ="from Address add";
Query query = session.createQuery(SQL_QUERY);
for(Iterator it=query.iterate();it.hasNext();){
Address add=(Address)it.next();

System.out.println("ID: " +add.getAddressId() );

}

session.close();
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
}

}
}
------------------------------------------------------------------------------------------

<?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="hiber.scr.Address" table="ADDRESS">
<meta attribute="class-description">This class contains the student's address
details.</meta>
<id name="addressId" type="long" column="id">
<generator class="identity" />
</id>
<property name="street" column="street"
/>
<property name="city" column="city"
/>
<property name="state" column="state"
/>
<property name="zipcode" column="zipcode"
/>
</class>
</hibernate-mapping>
-------------------------------------------------------------------------

package hiber.scr;

public class Address {
private long addressId;
private String street;
private String city;
private String state;
private String zipcode;

public Address(String street, String city, String state, String zipcode) {
this.street = street;
this.city = city;
this.state = state;
this.zipcode = zipcode;

}

public long getAddressId() {
return addressId;
}

public void setAddressId(long addressId) {
this.addressId = addressId;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZipcode() {
return zipcode;
}

public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
---------------------------------------------

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//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/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml" />
<mapping resource="student.hbm.xml" />
<mapping resource="Address.hbm.xml" />
</session-factory>
</hibernate-configuration>


-------------------------------------------------------------------------------------
thanks


Top
 Profile  
 
 Post subject: Re: from clause is not working
PostPosted: Mon Sep 21, 2009 5:28 am 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Try adding a no-arg constructor to the Address class.


Top
 Profile  
 
 Post subject: Re: from clause is not working
PostPosted: Mon Sep 21, 2009 5:52 am 
Newbie

Joined: Mon Sep 21, 2009 4:58 am
Posts: 2
thanks,
it works but can u plz explain why i have to use a zero constructor


Top
 Profile  
 
 Post subject: Re: from clause is not working
PostPosted: Mon Sep 21, 2009 6:06 am 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Hibernate requires all persistent classes to have a no-arg constructor, which is what it calls to instantiate the class.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.