hibernate mapping files easy to use formula

Not previously used formula, today Tried ,

Student and books or use the one-to-many relationship to illustrate:

Students.hbm.xml:

<hibernate-mapping>
  <class table="T_Students" name="com.hewill.model.Students">
    <id type="java.lang.String" column="id" length="32" name="id">
      <generator/>
    </id>
    <property name="bookCount" formula="(select count(*) from T_Books )"/>
    <property name="name" type="java.lang.String" column="name"/>
    <property name="sex" type="java.lang.String" column="sex"/>
    <property name="number" type="java.lang.String" column="number"/>
    <set inverse="true" cascade="all" name="books">
      <key column="student_id"/>
      <one-to-many/>
    </set>
  </class>
</hibernate-mapping>


Books.hbm.xml:

<hibernate-mapping>
  <class table="T_Books" name="com.hewill.model.Books">
    <id type="java.lang.String" column="id" length="32" name="id">
      <generator/>
    </id>
    <property name="bookname" type="java.lang.String" column="bookname"/>
    <many-to-one column="student_id" name="studentid"/>
  </class>
</hibernate-mapping>


Test code:

public static void main(String[] args) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Transaction tx = session.beginTransaction();
		
		Query query = session.createQuery("from Students where name='liu'");
		List<Students> list = query.list();

		for(Students students : list){
			System.out.println("name : "+students.getName());
			System.out.println("number : "+students.getNumber());
			System.out.println("bookCount : "+students.getBookCount());
		}

		tx.commit();
		session.close();

}


Results:

Hibernate: 
    select
        students0_.id as id0_,
        students0_.name as name0_,
        students0_.sex as sex0_,
        students0_.number as number0_,
        (select
            count(*) 
        from
            T_Books ) as formula0_ 
    from
        T_Students students0_ 
    where
        students0_.name='liu'
name : liu
number : 006
bookCount : 2


Hibernate implementation from the statement, we can see bookCount of Students through the implementation of property is a formula specified in "select count (*) from T_Books" to get the. bookCount field T_Students table in the database is not, it is a virtual column. Like this, when we use the database to complete the required number of terms, and would like to receive the results of this calculation is entirely possible to use formula.

the use of formula-line a lot of attention, not to mention at.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of hibernate mapping files easy to use formula

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Hibernate annotation using notebook

    These are the basic common @Entity --Declared an entity bean @Table(name="promotion_info") --For the entity bean mapping for the specified table (Table name ="promotion_info) @Id --Declare that the identifying attribute of the entity bean @GeneratedValue

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

  • Great collection of java interview topics

    1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract does n

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries