-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mehr SQL-Abfragen als erwartet
PostPosted: Mon May 27, 2013 10:28 am 
Newbie

Joined: Mon Jun 18, 2012 7:54 am
Posts: 6
Hallo Community,
ich habe folgendes Konstrukt:
ich habe folgende 2 Tabellen:
- Marktbegleiter
- Software

die Beziehung zwischen den beiden ist 1:n (1 Marktbegleiter -> n Software)
Meine Hibernate-Klassen habe ich folgendermaßen aufgebaut:
Marktbegleiter.java:
Code:
@Entity
@Table(name="MARKTBEGLEITER")
public class Marktbegleiter {

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((id == null) ? 0 : id.hashCode());
      result = prime * result + ((name == null) ? 0 : name.hashCode());
      result = prime * result + ((notiz == null) ? 0 : notiz.hashCode());
      return result;
   }
   
   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Marktbegleiter other = (Marktbegleiter) obj;
      if (id == null) {
         if (other.id != null)
            return false;
      } else if (!id.equals(other.id))
         return false;
      if (name == null) {
         if (other.name != null)
            return false;
      } else if (!name.equals(other.name))
         return false;
      if (notiz == null) {
         if (other.notiz != null)
            return false;
      } else if (!notiz.equals(other.notiz))
         return false;
      return true;
   }

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "ID")      
   private Integer id;
   
   @Column(name="NAME")
   private String name;
   
   @Column(name="NOTIZ")
   private String notiz;
   
//   @OneToMany
//   @JoinColumn(name="myMarktbegleiter", insertable=false, updatable=false)
//   private Set <Software> mySoftware = new HashSet<Software>();   

   @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="myMarktbegleiter", orphanRemoval=true)
   //@OrderBy("name")
   private Set <Software> mySoftware = new HashSet<Software>();      
   
   public Marktbegleiter() {
   }

   public Integer getId() {
      return id;
   }


   public void setId(Integer id) {
      this.id = id;
   }

   
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getNotiz() {
      return notiz;
   }

   public void setNotiz(String notiz) {
      this.notiz = notiz;
   }

   @Override
   public String toString() {
      return this.getName();
   }

   public Set <Software> getMySoftware() {
      return mySoftware;
   }

   public void setMySoftware(Set <Software> mySoftware) {
      this.mySoftware = mySoftware;
   }

}


Software:
Code:
@Entity
@Table(name="SOFTWARE")
public class Software {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "ID")      
   private Integer softwareID;
   
   @Column(name="Name")
   private String name;
   
   @Column(name="NOTIZ")
   private String notiz;
   
   @Column(name="VERSION")
   private String version;
   
   @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
   @JoinColumn(name ="Marktbleiterid", insertable=true, updatable=true)   
   private Marktbegleiter myMarktbegleiter;   
   
   
   public Software() {
   }

   public Integer getId() {
      return softwareID;
   }

   public void setId(Integer id) {
      this.softwareID = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
   
   @Override
   public String toString() {
      return this.getName();
   }

   public String getNotiz() {
      return notiz;
   }

   public void setNotiz(String notiz) {
      this.notiz = notiz;
   }

   public Marktbegleiter getMyMarktbegleiter() {
      return myMarktbegleiter;
   }

   public void setMyMarktbegleiter(Marktbegleiter myMarktbegleiter) {
      this.myMarktbegleiter = myMarktbegleiter;
   }

   public String getVersion() {
      return version;
   }

   public void setVersion(String version) {
      this.version = version;
   }
}


Soweit so gut.
Nun baue ich in meinem Programm folgende Abfrage auf:
Code:
    public List<?> loadSoftware(Vector<Marktbegleiter> marktbegleiter) {
        Criteria crit       = null;
        List<?> domainListe = null;
        if (marktbegleiter.size() > 0) {
            Session session = HibernateUtil.getSession();
            try {
                crit        = session.createCriteria(Software.class);   
                crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);   
                crit.addOrder(Order.asc("name"));
                crit.add(Restrictions.in("myMarktbegleiter", marktbegleiter));
                domainListe = crit.list(); 
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                session.close();
            }
        }
        return domainListe;
    }


An der Stelle:
Code:
domainListe                   = crit.list();


Werden nun aber folgende 2 SQL-Abfrage erzeugt (laut ausgabe von Hibernate):
Code:
    SELECT
        this_.ID AS ID23_1_,
        this_.Marktbleiterid AS Marktble5_23_1_,
        this_.Name AS Name23_1_,
        this_.NOTIZ AS NOTIZ23_1_,
        this_.VERSION AS VERSION23_1_,
        marktbegle2_.ID AS ID22_0_,
        marktbegle2_.NAME AS NAME22_0_,
        marktbegle2_.NOTIZ AS NOTIZ22_0_
    FROM
        SOFTWARE this_
    LEFT OUTER JOIN
        MARKTBEGLEITER marktbegle2_
            ON this_.Marktbleiterid=marktbegle2_.ID
    WHERE
        this_.Marktbleiterid IN (
            ?
        )
    ORDER BY
        this_.Name ASC


und
Code:
    SELECT
        mysoftware0_.Marktbleiterid AS Marktble5_22_1_,
        mysoftware0_.ID AS ID1_,
        mysoftware0_.ID AS ID23_0_,
        mysoftware0_.Marktbleiterid AS Marktble5_23_0_,
        mysoftware0_.Name AS Name23_0_,
        mysoftware0_.NOTIZ AS NOTIZ23_0_,
        mysoftware0_.VERSION AS VERSION23_0_
    FROM
        SOFTWARE mysoftware0_
    WHERE
        mysoftware0_.Marktbleiterid=?


Warum werden hier 2 Abfragen ausgeführt?
(bzw. in diesem Fall sind es 2. Wenn ich mehr Marktbegleiter Selektiere, wird es pro Marktbegleiter ein weiteres SQL)

Hoffe mein Problem ist verständlich und mir kann jemand helfen :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.