0

Hai i am new to Spring and i want to write join query between below mentioned two table Student Table contain student information and StudentDetail.java

@Entity
@Table(name = "student")
public class StudentDetail {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @OrderBy("name asc")
    public String name;
    public int age;

    @Column(unique = true)
    public long mobile;

    public String address;

    @Column(unique = true)
    public int doorNo;

    public String motherName;
    public String fatherName;
    public int sibiling;

    @Temporal(TemporalType.DATE)
    public Date joiningDate = new Date();

    @OneToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    @JoinColumn(name = "accountDetail")
    public Account account;

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public Date getJoiningDate() {
        return joiningDate;
    }

    public void setJoiningDate(Date joiningDate) {
        this.joiningDate = joiningDate;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public long getMobile() {
        return mobile;
    }

    public void setMobile(long mobile) {
        this.mobile = mobile;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getDoorNo() {
        return doorNo;
    }

    public void setDoorNo(int doorNo) {
        this.doorNo = doorNo;
    }

    public String getMotherName() {
        return motherName;
    }

    public void setMotherName(String motherName) {
        this.motherName = motherName;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public int getSibiling() {
        return sibiling;
    }

    public void setSibiling(int sibiling) {
        this.sibiling = sibiling;
    }
}

Account.java account table contain student id and name and student credit.

@Entity
@Table(name = "account")
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private float credit;

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

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

    public String getName() {
        return name;
    }

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

    public float getCredit() {
        return credit;
    }

    public void setCredit(float credit) {
        this.credit = credit;
    }

}

when running code it will return

[{"id":1,"name":"Admin","age":22,"mobile":9632587410,"address":"Chennai","doorNo":234,"motherName":"Admin"
                ,"fatherName":"Admin","sibiling":1,"joiningDate":"2016-06-02","account":null},{"id":3,"name":"Admin"
                ,"age":22,"mobile":9632587411,"address":"Chennai","doorNo":235,"motherName":"Admin","fatherName":"Admin"
                ,"sibiling":1,"joiningDate":"2016-06-01","account":null}]

but i need account table data please tell me i am very new to Spring

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
sathish
  • 59
  • 1
  • 6

1 Answers1

0

with hibernate you operate with entities, not queries or theirs intersections

First approach: if you want to create join table you can do it manually through

sessionFactory.createQuery (with HQL) or sessionFactory.createSqlQuery (Native SQL)

Second: create a new entity with a NamedQuery for select

Hibernate Named Query - join 3 tables

Third: use spring data to create repository with specific method name (spring data creates sql query based on method name) or use annotation @Query on the method of your's custom Repository to create complex queries (with JPQL)

Community
  • 1
  • 1
vexan
  • 88
  • 4