Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JPA @Column(unique = true) in a multi-tenant environment?

I want to convert my application to support multi-tenancy using shared tables (i.e. every table gets a tenant id). Obviously, I would not be able to use @Column(unique = true) any more, because it would enforce uniqueness across all tenants, which I don't want.

I'm using Glassfish 3.1.1 with EclipseLink. Is there a way make @Column(unique = true) force uniqueness per tenant (rather than per table). Or do I have to enforce this in the business logic?

like image 591
Theo Avatar asked Jul 22 '11 06:07

Theo


1 Answers

It is also possible to specify uniqueness constraint on then @Tableannotation, e.g.

@Table(name = "USERS", uniqueConstraints = @UniqueConstraint(columnNames = {"TENANT_ID", "username"}))
like image 70
Theo Avatar answered Sep 29 '22 08:09

Theo