Reputation: 2358
I am new to hibernate. I am designing an application in spring boot with JPA Hibernate.
I have database schema as :
CREATE TABLE IF NOT EXISTS `offers` (
`id` int(11) NOT NULL,
`detail` varchar(255) NOT NULL
`status` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `restaurants` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`status` varchar(255) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `rest_offer_map` (
`id` int(11) NOT NULL,
`rest_id` int(11) NOT NULL,
`offer_id` int(11) NOT NULL
`status` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I guess schema is clear. Now It could be the case that an restaurant exists without any offer. How do I create entities so that I could achieve left join. Also there is a column named status in every table, it needs to be taken care of (especially in left join as in that case status will be a join condition)
The o/p I want is list of all restaurants with the offers(in any), so left join is needed.
Upvotes: 0
Views: 96
Reputation: 172
You need to create the Restaurant entity as the owning side of the relationship as below
@Entity
@Table(name="restaurants")
public class Restaurant implements Serializable{
@Id
@Column(name="id")
private Long id;
@ManyToMany
@JoinTable(name="rest_offer_map",joinColumns=@JoinColumn(name="rest_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="offer_id",referencedColumnName="id"))
private Collection<Offer> offers;
.....
}
And the Offer entity as below
@Entity
@Table(name"offers")
public class Offer implements Serializable{
@Id
@Column(name="id")
private Long id;
ManyToMany(mappedBy="offers")
private Collection<Restaurant> restaurants;
......
}
Upvotes: 2