facebook

hi, select query in hibernate

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #257608 Reply

    hi,
    in i am using myeclipse 5.x editor, i am new to hibernate,

    i created a samplestudentproject,

    database table:-
    create table student(name varchar2(15), age number(2));
    insert into student values(‘kirankumar’, 25);
    insert into student values(‘rajeev’, 26);
    insert into student values(‘abcd’, 27);

    i reverse engineered and generated database class and mapping xml file,
    in com.org.hibernate.session package eclipse created
    1. AbstractStudent.java, which is an abstract class
    2. Student.java, which extends AbstractStudent.java
    3. Student.hbm.xml,
    4.StudentId.java, which contanis getter and setter methods of Student table fields name and age.
    5. HibernateSessionFactory file, for accessing Sesison.
    6. StudentInfo.java file created by me (from which i retrive rows from table)

    public static void main(String[] args) {
    try{
    Session session=HibernateSessionFactory.getSession();
    Transaction tx = session.beginTransaction();
    String select_query = “select std.name, std.age from Student as std”;
    Query query = session.createQuery(select_query);
    ……..

    here my confusion is how to use select statement to get table rows,

    if i execute then i am getting an error like this,

    org.hibernate.hql.ast.QuerySyntaxException: student is not mapped. [select name, age from student]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:157)
    at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
    at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
    at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:265)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3049)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2938)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
    at com.org.hibernate.student.StudentUpdate.main(StudentUpdate.java:19)
    Caused by: student is not mapped.
    … 20 more

    please sombody help me, 🙁

    regards,
    vemulakirankumar

    #257622 Reply

    Brian Fernandes
    Moderator

    Hi,

    When you reverse engineered these tables, did you choose “Update hibernate configuration with mapping file locaton” on page 1 of the RE Wizard?

    Your hibernate.cfg.xml file must include a line similar to the following, referencing the Student.hbm.xml file:

    <mapping resource="<path to file>/Student.hbm.xml" />

    This line should be done automatically if you enable the above option. Let us know if you require further assitance.

    Best,
    Brian.

    #257653 Reply

    hi, 🙂
    as you mentioned,
    Update hibernate configuration with mapping file locaton, i already did that, and mapping resource is automatically added by the myeclipse, and that is perfect,
    but the thing which confuse me is, acording to hibernate documentation,
    Student.java,Student.hbm.xml, SessionFactory class, and a clientclass which needs the database access,but MyEclipse ide is creating morethan that files, (ofcourse, someof them are helper classes).
    i will explain all my problem,

    Student.java file is the generated file by myelipse ide and Student.hbm.xml file is the mapping xml file to database. code of Student.java file is,
    This file extends AbstractStudent java class(why like this? what is the need):————–
    public class Student extends AbstractStudent implements java.io.Serializable {
    public Student(){}
    public Student(StudentId id) { super(id); }
    }
    ———Student.hbm.xml file——-
    <hibernate-mapping>
    <class name=”com.org.hibernate.student.Student” table=”STUDENT” schema=”JAVA”>
    <composite-id name=”id” class=”com.org.hibernate.student.StudentId”>
    <key-property name=”name” type=”java.lang.String”>
    <column name=”NAME” length=”10″ />
    </key-property>
    <key-property name=”age” type=”java.lang.Long”>
    <column name=”AGE” precision=”2″ scale=”0″ />
    </key-property>
    </composite-id>
    </class>
    </hibernate-mapping>
    ————————————-
    now AbstractStudent.java file looks like this:————————-
    public abstract class AbstractStudent implements java.io.Serializable {
    private StudentId id;public AbstractStudent() { }
    public AbstractStudent(StudentId id) { this.id = id; }
    public StudentId getId() { return this.id; }
    public void setId(StudentId id) { this.id = id; }
    }
    here is the StudentId.java file:———————(extends AbstractStudentId.java)
    public class StudentId extends AbstractStudentId implements java.io.Serializable {
    public StudentId() { }
    public StudentId(String name, Long age) { super(name, age); }
    ——AbstractStudentId.java ———-(in this file getter and setter files for Student table fields
    public abstract class AbstractStudentId implements java.io.Serializable {
    private String name; private Long age;
    public AbstractStudentId() { }
    public AbstractStudentId(String name, Long age) {
    this.name = name; this.age = age;
    }
    public String getName() { return this.name; }
    public void setName(String name) { this.name = name; }
    public Long getAge() { return this.age; }
    public void setAge(Long age) { this.age = age; }
    public boolean equals(Object other) {
    if ( (this == other ) ) return true;
    if ( (other == null ) ) return false;
    if ( !(other instanceof AbstractStudentId) ) return false;
    AbstractStudentId castOther = ( AbstractStudentId ) other;
    return ( (this.getName()==castOther.getName()) || ( this.getName()!=null && castOther.getName()!=null && this.getName().equals(castOther.getName()) ) )
    && ( (this.getAge()==castOther.getAge()) || ( this.getAge()!=null && castOther.getAge()!=null && this.getAge().equals(castOther.getAge()) ) );
    }
    public int hashCode() {
    int result = 17;
    result = 37 * result + ( getName() == null ? 0 : this.getName().hashCode() );
    result = 37 * result + ( getAge() == null ? 0 : this.getAge().hashCode() );
    return result;
    }
    ——-now i made a utility class (HibernateSessionFactory.java) to maintain sessions. It contains, getconfiguration(), getSesison(), closeSession(), setConfigFile(), rebuildSessionFactory() methods, which are used to support client sesson.

    now it is my class to access to database——
    i made class StudentIfo.java file to select all records from the Student table in database.
    public class StudentUpdate {public static void main(String[] args) {try{Session session=HibernateSessionFactory.getSession();
    Transaction tx=session.beginTransaction();
    StudentId sid= new StudentId(“hero”, new Long(23));
    Student std = new Student(sid);
    session.save(std); tx.commit();
    System.out.println(“success”); }catch(Exception e){ e.printStackTrace(); }}}
    —————————————-
    It works fine while inserting data in to data base tables,
    now how to retrive all the rows form the table.
    if write query like this —–select s.id from Student as s– then i am getting results like
    com.org.hibernate.student.Student.StudentId@70e6a22
    com.org.hibernate.student.Student.StudentId@7711118
    com.org.hibernate.student.Student.StudentId@7856581
    ……… this continues upto the no of rows present in Student table in database
    I think, it is retriving values from database and it is giving the address or any hash code representation of the values. then how to retrive the Name and Age values from this.

    Why i want to know because, i can write manual code without using myeclipse reverse engineering process, but for each and every table in a project writing all these code is a biggest process and takes long time,
    please give some code how to retrive all table rows.
    if possible you can mail to my mail id,
    vemulakirankumar@gmail.com
    regards,
    kirankumar vemula

    #257679 Reply

    Brian Fernandes
    Moderator

    Kiran,

    but MyEclipse ide is creating morethan that files, (ofcourse, someof them are helper classes).

    Yes, these are helper classes (mostly Data Access Objects), you can disable the generation of these classes by using the check boxes on Page 1 of the RE wizard. You might get a StudentId object if your primary key spans more than 1 column, to simplify use of the key.

    Student.java file is the generated file by myelipse ide and Student.hbm.xml file is the mapping xml file to database. code of Student.java file is,
    This file extends AbstractStudent java class(why like this? what is the need):————–

    During the course of application development, it is quite likely that you will change your table. At this time, you would want to regenerate your mapping file (hbm.xml) and the corresponding POJO (Student.java). When you regenerate your code, MyEclipse will overwrite AbstractStudent.java but will leave Student.java untouched. This allows you to add your own code to Student.java without losing it each time you regenerate the POJO from your table. Of course, if you don’t want this, you can delete the abstract class you have right now and disable the generation of abstract classes (this is an option on Page 1 of the RE wizard). This will cause Student.java to be overwritten each time you generate code. An Abstract class will not be generated.

    I think, it is retriving values from database and it is giving the address or any hash code representation of the values. then how to retrive the Name and Age values from this.

    What is being returned is a series of StudentId objects (since you probably have a compound key consisting of more than one columns) and the output of the toString() method is what you see. You’re getting the Id object because you’ve asked for “s.id” in your query. A query like “from Students” will give you a list of all students in your database, Student objects will be reuturned, you will have to call their getName() method (depends on the name of the column storing the name) to get the name.

    I would suggest reading some Hibernate documentation and tutorials as well.

    Hope this helps,
    Brian.

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: hi, select query in hibernate

You must be logged in to post in the forum log in