Reputation: 494
spring mvc + hibernate
I have two one table which contains user data.
now I want to make two class.
1. user
2. address -> because we can use it into many places.
and we have has-a relationship on that.
@Embedded
class Address
and
class user
{
@Autowired
Address address;
}
is it a good idea or bad?
due to this can I have to change in hibernate query?
Upvotes: 0
Views: 111
Reputation: 1717
It is good idea to implement has-a relationship.
create class Address with Embeddable annotation to define common column(properties)
@Embeddable
class Address{
String city;
String state;
}
use in User class has-a relationship
public class User {
@Embedded
private Address address;
}
You can query it like:
session.createQuery( "select u from User u where u.address.city=:city" ).setParameter( "city", 'ahmedabad' ).list();
or:
session.createQuery( "select u from User u where u.address.city in (:city)" ).setParameterList( "city", Arrays.asList( 'surat', 'ahmedabad' ) ).list();
Upvotes: 2