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.

分类:Java 时间:2009-06-17 人气:1768
分享到:
blog comments powered by Disqus

相关文章

  • java.lang Package Overview 2010-03-26

    1, interface java.lang.Appendable Can append (append) interfaces. Class implements this interface has an instance of an object can be an additional character or sequence of characters to their ability. Formatter hope to receive the output class must

  • Hibernate Query exception occurs java.lang.IllegalArgumentException 2011-04-25

    Encountered a problem today, record it. Hibernate to use a simple query, even reported exception java.lang.IllegalArgumentException Do not know why! ! ! ! Later been confirmed, because the database a field of type NUmber, but the type is int POJO And

  • java.lang.NoClassDefFoundError: bsh / TargetError 2009-04-05

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [F:\jBPM-WorkSpace\jBPM-ssh\WebRoot\WEB-INF\applicationContext.xml]: Invocation of init method fai

  • java.lang.NoSuchFieldError: INSTANCE 2010-02-23

    In Struts2 + Spring2.5 + ibatis integration problems is the lack of clear jar or packet, jar file conflicts. Desire to help. org.apache.catalina.core.StandardContext listenerStart : Exception sending context initialized event to listener instance of

  • java.lang.NoClassDefFoundError: org / jaxen / JaxenException (back Qinbo resolved) 2010-02-18

    [size = x-small] This is a continuous net friend gave me a few days, said the emergence of this exception message: java.lang.NoClassDefFoundError: org / jaxen / JaxenException In fact, the paper should be behind java.lang.NoClassDefFoundError other a

  • java.lang.UnsatisfiedLinkError: no swt-mozilla-gtk-3349 or swt-mozilla-gtk in sw 2010-03-13

    In linux eclipse of myeclipse appear java.lang.UnsatisfiedLinkError: no swt-mozilla-gtk-3349 or swt-mozilla-gtk in swt-mozilla-gtk in swt.library.path, java.library.path or the jar file in the error 1. Cause of the problem: SWT program using the Brow

  • JAVA based interface to object cloning to achieve java.lang.Cloneable 2010-04-23

    Let's look at the interpretation of JDK: java.lang.Cloneable A class implements the Cloneable interface to indicate to the java.lang.Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Inv

  • Caused by: java.lang.ClassNotFoundException: javax.persistence.Entity 2010-06-21

    Spring Hibernate integration of the following error when Serious : Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/clas

  • java.lang.IndexOutOfBoundsException 2010-07-27

    Using String.replaceAll (regex, replacement) method, when the replacement occurs backslash \, or dollar sign $ java.lang.IndexOutOfBoundsException No group occurs when abnormal, see api document is available java.util.regex . Matcher.quoteReplacement

  • tomcat5.0.27 + jdk1.4 + jsf appear java.lang.UnsupportedClassVersionError 2010-09-19

    Tomcat5.0.27 + jdk1.4 + jsf 2010-09-19 21:03:28 StandardContext[/balancer]org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News /

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.911 (s). 12 q(s)