So I'm trying to lay a onetomany relationship between 2 tables in a different database, I have the following mapping:
@OneToMany
@JoinTable(
name = "CountryEntityCountry",
joinColumns = @JoinColumn(name = "countryEntityId"),
inverseJoinColumns = @JoinColumn(name = "countryId")
)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public Set<Country> getDestinationCountries() {
return destinationCountries;
}
countryEntityId points back to my own entity CountryEntity, countryId points to a table in another database. Obviously I'd want hibernate to generate a clustered primary key, this is the sql generated by hibernate in MS SQL server:
USE [dbNcrt]
GO
/****** Object: Table [dbo].[CountryEntityCountry] Script Date: 05/06/2015 08:51:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CountryEntityCountry](
[countryEntityId] [numeric](19, 0) NOT NULL,
[countryId] [numeric](19, 0) NOT NULL,
PRIMARY KEY CLUSTERED
(
[countryEntityId] ASC,
[countryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [FG_DATA],
UNIQUE NONCLUSTERED
(
[countryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_R
OW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [FG_DATA]
) ON [FG_DATA]
GO
ALTER TABLE [dbo].[CountryEntityCountry] WITH CHECK ADD CONSTRAINT [FK6DAE065D46A920C0] FOREIGN KEY([countryEntityId])
REFERENCES [dbo].[CountryEntity] ([id])
GO
ALTER TABLE [dbo].[CountryEntityCountry] CHECK CONSTRAINT [FK6DAE065D46A920C0]
GO
Now for some reason hibernate is adding a unique constraint to the key on the MANY side. I don't mind having hibernate generate a unique constraint, but I'd appreciate it if it could be on the correct side, not on the many side, obviously I will have rows like:
- 1 245
- 1 234
- 2 245 -> Crash here already because of Hibernate's wrongfully generated unique constraint
Why is hibernate doing this? Is there something wrong with my mapping? I even tried adding explicitly unique=false to the countryId @JoinColumn, also tried adding the target entity explicitly, both of which hibernate seemed to have ignored.
Aucun commentaire:
Enregistrer un commentaire