0

I've got two entities AddressEntity and CompanyEntity.

Address entity

@Entity
@Table(name = "address")
public class AddressEntity implements Serializable {

    private static final long serialVersionUID = 6149442393833549397L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column(name = "city", nullable = false)
    private String city;

    @Column(name = "post")
    private String post;

    @Column(name = "street", nullable = false)
    private String street;

    @Column(name = "building_nr", nullable = false)
    private Integer buildingNr;

    @Column(name = "flat_nr")
    private Integer flatNr;

    // setters and getters
}

Company entity

@Entity
@Table(name = "company")
public class CompanyEntity implements Serializable {

    private static final long serialVersionUID = 3635072833730133590L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @OneToOne
    @Cascade(CascadeType.ALL)
    private AddressEntity address = new AddressEntity();

    @OneToMany(mappedBy = "company")
    @Cascade(CascadeType.ALL)
    private Set<DescriptionEntity> descriptions = new HashSet<DescriptionEntity>();

    @OneToMany(mappedBy = "company")
    @Cascade(CascadeType.ALL)
    private List<EmployeeEntity> employees = new ArrayList<EmployeeEntity>();

}

I want to select all addresses which are used by companies like in this SQL query SELECT a.city, c.name FROM address a INNER JOIN company c ON c.address_id=a.id; but using HQL query (I want to use JOIN, not WHERE). How can I do it? I want to select addresses using address table, not company table. I know that I can select addresses using company table like this select c.address.city, c.name from CompanyEntity c or using WHERE select a.city, c.name from CompanyEntity c, AddressEntity a WHERE c.address.id=a.id.

3
  • 1
    if you want to select addresses used by companies using address table u need to add relation between adress and company in address entity Commented Nov 1, 2013 at 11:17
  • @pepuch Why don't you write criteria? Commented Nov 1, 2013 at 11:26
  • I'm totally new in Hibernate. Can you please write your ideas how to solve this problem? Commented Nov 1, 2013 at 11:33

1 Answer 1

1
Criteria c = session.createCriteria(CompanyEntity.class);
c.createAlias("address", "address");
List<CompanyEntity> companies = c.list();

This will give you list of CompanyEntities having at least one AddressEntity.

If you add the following line:

c.criteria.setProjection(Projections.property("address"));

You will get a List of AddressEntities by doing c.list().

Tell me if you need something else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.