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:


  <class table="T_Students" name="com.hewill.model.Students">
    <id type="java.lang.String" column="id" length="32" name="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"/>


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

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());




        students0_.id as id0_,
        students0_.name as name0_,
        students0_.sex as sex0_,
        students0_.number as number0_,
            T_Books ) as formula0_
        T_Students students0_
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 imple

  • 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 failed; neste

  • 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 class

  • 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 based interface to object cloning to achieve java.lang.Cloneable 2010-04-23

    Let's look at the interpretation of JDK: Means: We usually need to operate on an object, but the object is two local operations, it must be when using the most primitive objects. This time there will need to Cloneable interfaces. And if we object ...

  • 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/classes/a

  • 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 / Redi

  • java.lang.NoSuchMethodError: antlr.collections.AST.getLine () I 2010-10-04

    Error Struts Problem Report Struts has detected an unhandled exception: # Messages: antlr.collections.AST.getLine()I File: org/hibernate/hql/ast/HqlSqlWalker.java Line number: 915 Stacktraces java.lang.reflect.InvocationTargetException sun.reflect.Na

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

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

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