ManyToMany properties
This may be a stupid question or may be asked, But i didnt find which helped me. Can anyone of you guys tell me what these properties described in ManyToMany relation do. And if there are any which i missed and should be used i开发者_运维问答n ManyToMany mapping please help me with that too.
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "customer_service",
joinColumns = {@JoinColumn(name = "customer_id")},
inverseJoinColumns = @JoinColumn(name = "service_id"))
This is description of a many-to-many relationship between (I am guessing) Customer
and Service
tables. Technically all attributes are optional so:
@ManyToMany
private Set<Customer> services;
in Service
would be fine (similar annotation on Customer
side if the relationship is bidirectional).
Now assuming you have a some basic relational databases design knowledge:
@JoinTable
describes a join table, the one used to store relationships between customer and service. Each row contains one reference to customer and one to service. This table will be namedcustomer_service
joinColumns
- definition of column(s) referencing the second side of the relationship -customer_id
inverseJoinColumns
- definition of column(s) referencing back to the starting side -service_id
in this example.
Join- and inverse join columns are symmetric - if you define the many-to-many relationship on the other side you switch them. Otherwise everything will work from Java perspective but Hibernate will create a foreign key from customer_id
to Service
table and vice-versa and will insert values accordingly.
精彩评论