I saw this question on StackOverflow and it looked like it could use some loving, especially as I'm having a similar issue and the @JoinColumnsOrFormula documentation is a bit scarce. http://stackoverflow.com/questions/18825158/hibernate-how-to-move-from-joincolumns-to-joincolumnsorformulas-to-handle-nul
I'm using Java and I'n having some issues with my hibernate mappings since all the tables use unique keys and not primary keys and I haven't managed to convince our DBA yet to add a PK column.
Currently I have the following:
Code:
@OneToMany
@JoinColumns({
@JoinColumn(name = "CITY", referencedColumnName = "CITY"),
@JoinColumn(name = "STREET", referencedColumnName = "STREET"),
@JoinColumn(name = "NUMBER", referencedColumnName = "NUMBER"),
@JoinColumn(name = "BUSNUMBER", referencedColumnName = "BUSNUMBER")})
private List<Address> addresses;
Now this works fine if all the fields are filled in, but I have some issues with the PersistentBag as soon as busnumber for the object is empty. After checking the Hibernate documentation I stumbled upon the @JoinColumnsOrFormulas annotation, but it's documentation seems a bit limited and even with a decent bit of googling I couldn't find any samples which actually join multiple columns as the name would imply.
is the way to go:
Code:
@OneToMany
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.CITY FROM A a WHERE a.CITY = CITY )", referencedColumnName = "CITY ")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.STREET FROM A a WHERE a.STREET = STREET)", referencedColumnName = "STREET")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.NUMBERFROM A a WHERE a.NUMBER = NUMBER)", referencedColumnName = "NUMBER")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.BUSNUMBER FROM A a WHERE a.BUSNUMBER = BUSNUMBER or (a.BUSNUMBER is null AND BUSNUMBER is null))", referencedColumnName = "BUSNUMBER"))}
private List<Address> addresses;
or should I put it in a single @JoinFormula? I have currently written the above, but I can't test it yet as a reload is occurring on our DB.
edit: I have tried the above, but it seems that is note the correct way to go as I just got exceptions. Any advice is welcome.