-->
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: Why hibernate creates optional interrogations, and more?
PostPosted: Mon Jun 13, 2011 2:05 pm 
Newbie

Joined: Sun Jun 12, 2011 1:43 pm
Posts: 10
My problem is that i'm trying to make a simple hql but the results are not what i want.
When i check the logs it looks like hibernate added some more interrogations. Please tell me why and how to fix this. Does it has something to do with the "cascade=cascadeType" ?

EDIT: Ok, i'm sorry, i do get the intended results (i forgot to overwrite toString ) but i still don't understand why it creates additional queries.

Project.java
***********
Code:
@Entity
@Table(name="PROJECTS")
public class Project extends Organization {
   
   static private final Map<String,Project> allProjects = new HashMap<String,Project>();
   private Map<String, Document> documents;
   private Long id;
   
   public void setDocuments(Map<String, Document> documents) {
      this.documents = documents;
   }
   
   @OneToMany(targetEntity=net.translators.logic.documents.Document.class,
         mappedBy="project")
   @MapKey(name="id")
   public Map<String, Document> getDocuments() {
      if (documents ==null)
          documents= new HashMap<String, Document>();
      return documents;
   }
   
   public void addDocument(Document doc){
      documents.put(doc.getId().toString(), doc);
   }
   
   public void setId(Long id) {
      this.id = id;
   }

   @Id @GeneratedValue
   @Column(name="project_id")
   public Long getId() {
      return id;
   }

   public static Map<String,Project> getAllProjects() {
      return allProjects;
   }

   public String toString(){
      return "{ID:"+getId()+"}{Name:"+getFirstName()+"}";
   }

}


Document.java
********
Code:
@Entity
@Table(name="DOCUMENTS")
public class Document {
   private String name;
   private int[] translateTo;
   private int writtenLang;
   private Project project;
//   private List<Content> content;
   private Long id;
   
   public void removeTranslationLang(int lang){
      
   }
   public void setDocLang(int lang){
      
   }
   public void setId(Long id) {
      this.id = id;
   }
   
   @Id @GeneratedValue
   @Column(name="id")
   public Long getId() {
      return id;
   }
   public void setName(String name) {
      this.name = name;
   }
   
   @Column(name="name")
   public String getName() {
      return name;
   }
   
   public void setProject(Project project) {
      this.project = project;
   }
   
   @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="project_fk")
   public Project getProject() {
      return project;
   }

}



Organization.java
**************
Code:
@MappedSuperclass
public class Organization extends Identity{

   private User[] users;
   private User president;
   private String description;
   
   public void setUsers(User[] users) {
      this.users = users;
   }
   
   public User[] getUsers() {
      return users;
   }
   
   public void setPresident(User president) {
      this.president = president;
   }
   
   @ManyToOne
   public User getPresident() {
      return president;
   }
   
   public String toString(){
      return "Users:<not yet>"+"; Name:"+getFirstName()+"; President:"+president.getFirstName();
   }

   public void setDescription(String description) {
      this.description = description;
   }

   public String getDescription() {
      return description;
   }
   
}



User.java
*********
Code:
@Entity
@AttributeOverrides({
@AttributeOverride(name = "firstName",
   column =@Column(name = "first_name")
   ),
@AttributeOverride(name = "lastName",
   column =@Column(name = "last_name")
   ),
})
   
@Table(name="USERS")
public class User extends Identity implements Serializable{

   private static final long serialVersionUID = 5315262679292234377L;
   private static Map<String,User> allusers = new HashMap<String,User>();
   private static Map<String, String> cidVSbid = new HashMap<String,String>();
   
   private Long id;
//   private User nextUser;
   private String pass;
   private int karma;

   
   @Id
   @GeneratedValue
   @Column(name="user_id")
   public Long getId(){
      return id;
   }
   
   public void setId(Long id){
      this.id = id;
   }
      
   public int getKarma(){
      return karma;
   }
   
   public void setKarma(int val){
      this.karma = val;
   }
   
   public String toString(){
      return super.toString()+"{Karma:"+getKarma()+"}";
   }


   public static Map<String,User> getAllusers() {
      return allusers;
   }
   
   
   public void setPass(String pass) {
      this.pass = pass;
   }

   @Column(name = "pass")
   @NotNull @NotEmpty
   @Size (min=5)
   public String getPass() {
      return pass;
   }

   public static Map<String, String> getCidVSbid() {
      return cidVSbid;
   }

}


the code
*******
Code:
         System.out.println("Yes, here.");
         documentList= (List<Document>)s.createQuery("from Document d where d.project="+project_id).list();
         System.out.println(documentList.get(0).toString());


LOGS
********
Code:
Yes, here.
Hibernate:
    /*
from
    Document d
where
    d.project=1 */ select
        document0_.id as id2_,
        document0_.name as name2_,
        document0_.project_fk as project3_2_
    from
        DOCUMENTS document0_
    where
        document0_.project_fk=1
Hibernate:
    /* load net.translators.logic.organizations.Project */ select
        project0_.project_id as project1_1_1_,
        project0_.email as email1_1_,
        project0_.first_name as first3_1_1_,
        project0_.last_name as last4_1_1_,
        project0_.description as descript5_1_1_,
        project0_.president_user_id as president7_1_1_,
        project0_.users as users1_1_,
        user1_.user_id as user1_0_0_,
        user1_.email as email0_0_,
        user1_.first_name as first3_0_0_,
        user1_.last_name as last4_0_0_,
        user1_.karma as karma0_0_,
        user1_.pass as pass0_0_
    from
        PROJECTS project0_
    left outer join
        USERS user1_
            on project0_.president_user_id=user1_.user_id
    where
        project0_.project_id=?
net.translators.logic.documents.Document@1737b9f


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.