I have an entity (Entity1) with an id and an string as a unique fields:
@Id
@GeneratedValue
@Column(name = "entity1_id")
private long entity1_id;
@Column(name = "address", nullable = false, unique = true)
private String address;
There is a relation with another entity (Entity2) in this way:
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "middle_table", joinColumns = @JoinColumn(name = entity2_id), inverseJoinColumns = @JoinColumn(name = "entity1_id"))
private List<Entity> entity1List= new ArrayList<Entity>();
FetchType is eager, so when I get an Object2 from the database, it comes with the complete list of Entity1.
The problem is that the address
field is supposed to be in lowercase, but because of a migration problem, we have some cases where an address contains some capital letters. In those cases Entity2.entity1List
array contains 1 object, which is null, because it can not be found in the database.
When the middle table is updated, I get this exception:
ERROR: null value in column "entity1_id" violates not-null constraint {prepstmnt 892492234 INSERT INTO public.middle_table(entity2_id, entity1_id) VALUES (?, ?) [params=?, ?]} [code=0, state=23502]
To make it clearer, let´s say that I have in the database: Test@test.com
, but it should be as well identified as test@test.com
.
EDIT
I have just realized that the whole explanation is not necessary. I can explain my problem in an easier way:
In my table I have an entry: Test@test.com
. I wan this query:
select * from my_table where address = 'test@test.com'
To retrieve the result.
In my table I have an entry: Test@test.com. I wan this query:
select * from my_table where address = 'test@test.com'
Well, if you put it this way, you may as well restructure the query to:
select * from my_table where LOWER(address) = LOWER(:email)
For this query to be efficient, you'll need a function-based index on your database — otherwise it's O(n) complex.