In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the role of Hibernate Formula. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1.Hibernate Formula action
Citing the explanation in the Hibernate annotations technical documentation can well illustrate the role of @ Formula, but it really doesn't explain how to use it, and the examples given are useless, which cost me hours!
The function of Hibernate Formula is to use a query statement to dynamically generate the properties of a class. For example, the inbox shows several unread email numbers after java eye login, which is a select count (*). Constitutes a virtual column instead of a field stored in the database. To put it in a more standard way: sometimes you want the database, not JVM, to do some calculations for you, or you may want to create some kind of virtual column, and you can use sql fragments instead of mapping attributes to (physical) columns. This property is read-only (the attribute value is obtained by the formula). Formula can even contain SQL subqueries
Is Formula really that powerful? Indeed, it is very good and powerful, saving a lot of code!
two。 Use Formula
Package aa; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.Formula / * comments must be on the property, if there are any comments on the method Then @ Formula will fail * @ author Kunming hummingbird software * @ version 0.1.0 2008-7-15 06:09:38 * / @ Entity @ Table (name = "user", catalog = "test") public class User {@ Id @ GeneratedValue (strategy = IDENTITY) private int id @ Formula ("(select COUNT (*) from user)") private int count; public int getId () {return id;} public void setId (int id) {this.id = id;} public int getCount () {return count } public void setCount (int count) {this.count = count;}} package aa; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.Formula / * the annotation must be on the attribute. If there is any comment on the method, @ Formula will fail * @ author Kunming hummingbird software * @ version 0.1.0 2008-7-15 06:09:38 * / @ Entity @ Table (name = "user", catalog = "test") public class User {@ Id @ GeneratedValue (strategy = IDENTITY) private int id Formula ("(select COUNT (*) from user)") private int count; public int getId () {return id;} public void setId (int id) {this.id = id;} public int getCount () {return count;} public void setCount (int count) {this.count = count;}}
Database tables: Sql cod
CREATE TABLE `test`.`user` (`id` int (10) unsigned NOT NULL auto_increment, PRIMARY KEY USING BTREE (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
Details 1. With @ Formula your annotation must be on the property, and if there is a comment on the method, @ Formula will be invalidated. I have experimented with this, such as changing the above java file to:
Java code
Package aa; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.Formula / * the annotation must be on the attribute. If there is any comment on the method, @ Formula will fail * @ author Kunming hummingbird software * @ version 0.1.0 2008-7-15 06:09:38 * / @ Entity @ Table (name = "user", catalog = "test") public class User {private int id Formula ("(select COUNT (*) from user)") private int count; @ Id @ GeneratedValue (strategy = IDENTITY) public int getId () {return id;} public void setId (int id) {this.id = id } public int getCount () {return count;} public void setCount (int count) {this.count = count;}} package aa; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table The import org.hibernate.annotations.Formula; / * annotation must be on the attribute. If there is any comment on the method, @ Formula will fail * @ author Kunming hummingbird software * @ version 0.1.0 2008-7-15 06:09:38 * / @ Entity @ Table (name = "user", catalog = "test") public class User {private int id Formula ("(select COUNT (*) from user)") private int count; @ Id @ GeneratedValue (strategy = IDENTITY) public int getId () {return id;} public void setId (int id) {this.id = id;} public int getCount () {return count;} public void setCount (int count) {this.count = count;}}
So @ Formula can't run! I was confused by the official documents of Hibernate in front of me.
Details 2. Since @ Formula is a virtual column, you don't need to create this column in the database, and hibernate will ignore it if there is a column. The user in the above example does not have a count column.
The detail 3.sql statement must be written in (), which has been said before.
Details 4. If there is a where subquery, then the table needs an alias, such as select COUNT (*) from user where id=1 is wrong
And select COUNT (*) from user u where u.id=1 is correct.
Details 5. @ Formula should be supported as long as you execute statements in the sql console of the database and use a table alias.
Thank you for reading! This is the end of this article on "what is the role of Hibernate Formula?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.