-->
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.  [ 5 posts ] 
Author Message
 Post subject: populating instance variables based on sql
PostPosted: Fri Sep 26, 2008 6:32 am 
Newbie

Joined: Fri Sep 26, 2008 6:20 am
Posts: 4
I have this entity

Code:
@Entity
@Table(name="operators")
public class LpOperator implements Serializable{
 
  private int id;
 
  private String postCode;
 
  private String operatorName;
 
  private String operatorRegistrationBumber;
 
  private int operatorType;
 
  private Set<LpVehicle> vehicles;

The vehicles does not have any direct mapping with the operators. I can only populate the vehicle list based on sql. My question is, is there an annotation or a way of making hibernate use sql to populate the vehicle list when LpOperator is being populated?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 10:48 am 
Newbie

Joined: Mon Oct 15, 2007 5:05 am
Posts: 3
Well let me ask a question ... after fetching your operator how are you going to bring the dependent vehicles. If there is some key n which you are going to fetch the vehicles then you can use the same key to build an association from operator to vehicle. Something like

Code:
@OnetoMany
@joinColumn("Key")
Set<Vehicle> getVehicles()
{}

For this you don't need a physical relationship from the database end. All you need to do is build this relationship among the entities.

Let me know if it works.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 11:12 am 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
What do you mean by
Quote:
based on sql
?

http://www.hibernate.org/hib_docs/annot ... e/#d0e1177

Does this help?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 12:04 pm 
Newbie

Joined: Fri Sep 26, 2008 6:20 am
Posts: 4
This is an example of how our Delphi developers pull some bits of the licence details and vehicle details of an operator. I'm designing a model to suit an existing database, so i don't have a lot of room

Code:
Select
lic_id,
lic_numb,
veh_id,
Veh_reg_no,
veh_no_passengers

from Licences

left outer join lictype on lty_id = lic_typ_id
left outer join clients on cli_id  = lic_cli_id_resp
left outer join vehicles on veh_id = lic_veh_id

where lic_opr_id = 5

#opr_id = 5


key opr=operator
veh= vehicles
cli=clients

the rest is obvious

Does it mean I always have to select licences. This is how my model looks now

Code:
@Entity
@Table(name="operators")

public class LpOperator implements Serializable{
 
  private int id;
 
  private String postCode;
 
  private String operatorName;
 
  private String ward;
 
  private String operatorRegistrationBumber;
 
  private int operatorType;
 
  private Set<LpVehicle> vehicles;
 
  private Set<LpLicence> licences;

  public LpOperator() {
  }

  @Id
  @Column(name="opr_id")
  public int getId() {
    return id;
  }

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

  @Column(name="opr_registration_number")
  public String getOperatorRegistrationBumber() {
    return operatorRegistrationBumber;
  }

  public void setOperatorRegistrationBumber(String operatorRegistrationBumber) {
    this.operatorRegistrationBumber = operatorRegistrationBumber;
  }

  @Column(name="opr_off_postcode")
  public String getPostCode() {
    return postCode;
  }

  public void setPostCode(String postCode) {
    this.postCode = postCode;
  }

  @Column(name="opr_name")
  public String getOperatorName() {
    return operatorName;
  }

  public void setOperatorName(String operatorName) {
    this.operatorName = operatorName;
  }

  @Column(name="opr_type")
  public int getOperatorType() {
    return operatorType;
  }

  public void setOperatorType(int operatorType) {
    this.operatorType = operatorType;
  }

  @Column(name="opr_type")
  public String getWard() {
    return ward;
  }

  public void setWard(String ward) {
    this.ward = ward;
  }
 
  @ManyToMany(
      targetEntity=LpVehicle.class,
      mappedBy="vehicles",
      fetch=FetchType.EAGER
  )
  @JoinTable(name="client"??,
   Dont know what to put here       
              )
  public Set<LpVehicle> getVehicles() {
    return vehicles;
  }

  public void setVehicles(Set<LpVehicle> vehicles) {
    this.vehicles = vehicles;
  }

  @OneToMany(
      mappedBy="operator",
      targetEntity=LpLicence.class,
      fetch=FetchType.EAGER
  )
  public Set<LpLicence> getLicences() {
    return licences;
  }

  public void setLicences(Set<LpLicence> licences) {
    this.licences = licences;
  }


  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final LpOperator other = (LpOperator) obj;
    if (this.id != other.id) {
      return false;
    }
    return true;
  }

  @Override
  public int hashCode() {
    int hash = 3;
    hash = 53 * hash + this.id;
    return hash;
  }
 
 
}



Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 2:05 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
I think the problem is that you aren't allowed to use mappedBy and JoinTable together.

http://www.hibernate.org/hib_docs/annot ... e/#d0e1529

You need to decide which side of the many to many relationship is the 'owner' (probably the operator).
Actually, I'm not sure if your relationship is truly a many to many relationship. Without a join table I think you're looking at a one to many relationship. One operator can have a bunch of vehicles? Can a vehicle belong to multiple operators? How is this relationship represented in your DB? I didn't see anything about operators in the query you provided.


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

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.