MR AND
MR AND

Reputation: 406

Update a column in one table when it is linked with another table in hibernate

I want to update address column in User class using HQL. address_id is linked to Address using structure shown below. I want help on how to do so using HQL .

 class User{
    int id;
    String firstName;
    String lastName;

    @OneToOne
    @JoinColumn(name="address_id")
    @NotFound(action = NotFoundAction.IGNORE)
    private Address  address ;
}

class Address{
    @Id
    @Column(name="address_id")
    int address_id;
    String city;
    String state;   
}

This is what my HQl query looks like but I am not sure how to pass whole object as query parameter

String hql = "update from User set id= null where address=:address ";

Upvotes: 0

Views: 646

Answers (1)

Milan Desai
Milan Desai

Reputation: 1306

You can use annotation @Query and @Modifying

Retrieve Address object by,

Address address = select address from address where address.id=XYZ 

and latter use address object however you want. ie.

@Modifying
@Query("update from User set id= null where address=?1";
void updateAddress(address);
// fyi here ?1 rectifies argument id 

Upvotes: 1

Related Questions