This seems to be a pretty simple question, or at least a simple answer. But - I'm really not a database guy, and I'm still pretty far from the Hibernate learning curve. However, here is the setting:
Consider a many-to-many unidirectional relationship between two objects: from Footo Bar:
(forgiving any typos below is obviously a simplification of the actual code)
FooDTO.java:
@Entity
@Table(name = "MyDB.dbo.Foo")
class FooDTO implements Serializable
{
private int id;
private String name;
private Set<BarDTO> bars = new HashSet<BarDTO>();
...
@Fetch(FetchMode.JOIN)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "MyDB.dbo.FooBar",
joinColumns = { @JoinColumn(name = "fooId") },
inverseJoinColumns = { @JoinColumn(name = "barId") })
public Set<BarDTO> getBars()
{
return bars;
}
public void setBars(Set<Bar> bars)
{
this.bars = bars;
}
}
BarDTO.java:
@Entity
@Table(name = "MyDB.dbo.Bar")
class BarDTO implements Serializable
{
private int id;
private String name;
...
}
On the TSQL side, the connection table is configured as follows:
CREATE TABLE [dbo].[FooBar](
[id] [int] NOT NULL IDENTITY PRIMARY KEY,
[fooId] [int] NOT NULL,
[barId] [int] NOT NULL,
CONSTRAINT fk_FooBar_FooId FOREIGN KEY (fooId) REFERENCES [dbo].[Foo](id),
CONSTRAINT fk_FooBar_BarId FOREIGN KEY (barId) REFERENCES [dbo].[Bar](id),
) ON [PRIMARY]
END
If I try to delete a BarDTO, I get a ConstraintViolationExceptionbecause I did not delete the first row in the connection table (duh).
Questions:
- Hibernate ,
Bar? ? - ,
Foo, Bar, Bar Foo Bar s?
, , NamedQuery, API , , . , :
SELECT f FROM FooDTO f INNER JOIN ??? WHERE f.id = ???
, barId FooBar, . ( , - ?)