Article From:https://segmentfault.com/q/1010000011142995
Question:

SSHFor example, a one-to-many query for two tables, first-level and second-level relational tables, uses set sets, but the query results in duplicate data…. Look at the answer ~ ~ ~

Primary entity

public class subject01 {
    private int sub_id;
    private String sub_name;
    private String sub_introduce;
    private String sub_url;

    // Two level information setPrivate Set< subject02> subject02Set = new HashSet< subject02> ();//set get...}

Mapping file

Two level entity

public class subject02 {
    private int sub2_id;
    private String sub2_name;
    private int sub_id;
}

Mapping file

Implementation layer

public class subject_sql_Implements {
    private SessionFactory sessionFactory;
    //    set get...
    // One or two level topic inquiryPublic List< subject01> selectOneAndTwo () {Session session = sessionFactorY.openSession ();List< subject01> list = new ArrayList< subject01> ();/ / thisThe result of the query is a parent topic that corresponds to multiple sub topics (set by set).String Hql = "from subject01 S1 join s1.subject02Set";Query query = session.createQuery (Hql);/ / here we will query the result set to assign to the obj set, while the generic is the array type (subject02Set has the object number.Group), similar to a two-dimensional arrayList< Object[]> obj = query.list ();For (int i = 0; I < obj.size (); i++) {/ / [0] to get the first entity querySubject01 sub01 = (subject01) obj.get (I) [0];/ / (the same only add the last one)If (I = = obj.size () - 1) {List.add (sub01);Break;}/ / left part: get the ID of the parent subject/ / the right part: the ID in the obj collection also gets the parent theme./ / the left and right sides are matched and repeated items are dischargedIf (sub01.getSub_id () = = ((subject01) obj.get (I + 1) [0]).GEtSub_id ()) {Continue; / / / / repeat, continue to circulate} else {/ / / / no repetitionNow, add the dataList.add (sub01);}}Return list;}/ / /Omit the other methods}

In the database, only 2 related data are stored on the first level data, but duplicate data will appear in the query.

Updated code

// One or two level topic inquiryPublic List< subject01> selectOneAndTwo () {Session session = sessionFactory.oPenSession ();String Hql = "from subject01 S1 left join fetch s1.subject02Set";QUery query = session.createQuery (Hql);List< subject01> obj = query.list ();/ / remove the previous heavy codeReturn obj;}

Display data

Answer 0:

The revision is as follows:
String Hql = “from subject01 s1 left join fetch s1.subject02Set”;
Query query = session.createQuery(Hql);
List<subject01> obj = query.list();
//If you need to get a two level entity, obj.get (0).GetSubject02Set () is the two level entity under this level entity.
Can you see if you can?
Why do I repeat it? Look at some of the articles I used to learn from hibernate.

One of them is the location of the following pictures.

To update:
I looked at my previous article (2) in the above address: “the urgent left external connection” this chapter), one to many cases, using String Hql = “from subject01 S1 left join fetch s1.Subject02Set “; the statement is still repeated,
Check it out and use HashSet to filter subject01. Hibernate I don’t have to use it for a long time.

//Rewrite the equals and hashCode () methodsClass subject01 {Private int sub_id;Private String sub_name;PRIvate String sub_introduce;Private String sub_url;@OverridePublic int hashCODE () {Final int prime = 31;Int result = 1;Result = prime * result + sub_ID;Return result;}@OverridePublic Boolean equals (Object obj) {If(this = = obj)Return true;If (obj = = null)Return false;If(getClass ()! = obj.getClass ())Return false;Subject01 other = (subject01) obj;If (sub_id! = other.sub_id)Return false;Return true;}/ / twoLevel information setPrivate Set< subject02> subject02Set = new HashSet< subject02> ();Public INT getSub_id () {Return sub_id;}Public void setSub_id (int sub_id) {This.sub_id = sub_id;}Public String getSub_name () {Return sub_name;}PubLIC void setSub_name (String sub_name) {This.sub_name = sub_name;}Public StringGetSub_introduce () {Return sub_introduce;}Public void setSub_introduce (String s)Ub_introduce) {This.sub_introduce = sub_introduce;}Public String getSub_url () {Return sub_url;}Public void setSub_url (String sub_url) {This.sub_url =Sub_url;}Public Set< subject02> getSubject02Set () {Return subject02Set;}Public void setSubject02Set (Set< subject02> subject02Set) {This.subject02SeT = subject02Set;}/ / / / filter redundant dataString Hql = "from subject01 S1 left join fetch s1.subject02Set"";Query query = session.createQuery (Hql);List< subject01> obj = query.list ();Set< subject01> set = new HashSet< subject01> (obj);Obj = new ArrayList< subject01> (set);/ / then proceed to your ownThe logic belowFor (int i = 0; I < obj.size (); i++) {...}

I suggest you take a look at the relevant articles on my hibernate address above.

Answer 1:

The first statement is that no HQL has been used

 List<Object[]> obj = query.list(); 

Do not repeat itself.
hqlParsing is also SQL, SQL, you two tables inner join after completion of the 5 records, this is not repeated?
2Record inner join 5, do you see inner 5 join?
Write a inner join for yourself and try it


When I was invited to come to reply, I was kicked. I found that many people do not like to praise.

Answer 2:

Write the answer, tell me what I didn’t change when I saw the code last night. This morning, I wanted to continue to write the project and find out such a BUG, and check it for a day. Take your heart.

subject01

package Entity;
public class subject01 {
    private int sub_id;
    private String sub_name;
    private String sub_introduce;
    private String sub_url;

    // Two level information setPrivate Set< subject02> subject02Set = new HashSet< subject02> ();@OverridEPublic int hashCode () {Final int prime = 31;Int result = 1;Result =Prime * result + sub_id;Return result;}@OverridePublic Boolean equals (ObjeCT obj) {If (this = = obj)Return true;If (obj = = null)RetUrn false;If (getClass ()! = obj.getClass ())Return false;Subject01 othEr = (subject01) obj;If (sub_id! = other.sub_id)Return false;Return tRue;}//set get}

Business implementation code

// One or two level topic inquiryPublic List< cms_subject01> selectOneAndTwo () {Session session = sessionFaCtory.openSession ();String Hql = "from cms_subject01 S1 left join fetch s1.subject02Set";Query query = session.createQuery (Hql);List< cms_subject01> obj = query.list ();Set< cms_subject01> set = new HashSet< cms_subject01> (obj);Obj = new ArrAyList< cms_subject01> (set);Return obj;}

Answer 3:

You can refer to this
http://www.zuidaima.com/blog/…

Answer 4:

I didn’t read your code. If I came across this logic, I would use leftjoin.

Leave a Reply

Your email address will not be published. Required fields are marked *