Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Hibernate to deal with views without primary keys

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Hibernate to deal with views without primary keys". In daily operation, I believe many people have doubts about how to use Hibernate to deal with views without primary keys. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to use Hibernate to deal with views without primary keys". Next, please follow the editor to study!

Details of the view

SELECT TEST_FLIGHT_TIME AS TESTTIME,JIHAO AS JIHAO,'1' AS TYPE,FP_ID,REF_ID FROM S_FLIGHT_PLAN-- 1: host check time

UNION

SELECT INSPECT_TIME AS TESTTIME,JIHAO AS JIHAO,'2' AS TYPE,FP_ID,REF_ID FROM S_FLIGHT_PLAN-2: mainframe flight test time

UNION

SELECT TEST_FLIGHT_TIME1 AS TESTTIME,STANDBY_PLANE AS JIHAO,'3' AS TYPE,FP_ID,REF_ID FROM S_FLIGHT_PLAN-3: backup machine check time

UNION

SELECT INSPECT_TIME1 AS TESTTIME,STANDBY_PLANE AS JIHAO,'4' AS TYPE,FP_ID,REF_ID FROM S_FLIGHT_PLAN-4: backup aircraft test flight time-test flight time, test aircraft number, test flight type, flight mission id, dispatch mission id

A) five TSETTIME,JIHAO,TYPE,FP_ID,REF_ID fields are used in this view, and since each field is not unique, there is no primary key

As hibernate deals with all data, basically there must be a primary key, so there will be trouble when pressing to create entity classes, with the following solution.

Create a new primary key class, use several properties as the federated primary key, decorate with the @ Embeddable annotation, put several properties in, write the get,set method, and override the equals () and hashCode () methods.

@ Embeddable public class VTestFlightKey implements Serializable {private static final long serialVersionUID = 1L return REF_ID; private Date TESTTIME;/**JIHAO flight number / private String JIHAO; / * TYPE * / private String TYPE; / * FP_ID Flight Plan id * / private String FP_ID;private String REF_ID;public String getREF_ID () {return REF_ID;} public void setREF_ID (String rEF_ID) {REF_ID = rEF_ID;} public String getFpid () {return FP_ID } public void setFpid (String fpid) {this.FP_ID = fpid;} public Date getTestTime () {return TESTTIME;} public void setTestTime (Date testTime) {this.TESTTIME=testTime;} public String getJihao () {return JIHAO;} public void setJihao (String jihao) {this.JIHAO=jihao;} public String getType () {return TYPE;} public void setType (String type) {this.TYPE=type } @ Overridepublic int hashCode () {return this.TESTTIME.hashCode ();} @ Overridepublic boolean equals (Object o) {if (o instanceof VTestFlightKey) {VTestFlightKey key = (VTestFlightKey) o If (this.TESTTIME.equals (key.getTestTime ()) & & this.JIHAO.equals (key.getJihao ()) & & this.TYPE.equals (key.getType ()) & & this.FP_ID.equals (key.getFpid ()) & & this.REF_ID.equals (key.getREF_ID ()) {return true;}} return false;}}

The primary key class, as the ID attribute of the main class, is modified with @ Id annotation, and the attributes in the primary key class that have been used as federated primary keys are no longer written in the main class.

@ Entity @ Table (name= "V_TEST_FLIGHT") public class VTestFlight {private static final long serialVersionUID = 1L; / / since all fields are used as federated primary keys, the fields are in the primary key class VTestFlightKey @ Id private VTestFlightKey vTestFlightKey; public VTestFlightKey getvTestFlightKey () {return vTestFlightKey;} public void setvTestFlightKey (VTestFlightKey vTestFlightKey) {this.vTestFlightKey = vTestFlightKey }} at this point, the study on "how to use Hibernate to deal with views without primary keys" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report