Ken Hirakawa
Ken Hirakawa

Reputation: 8251

hibernate @Column only if column exists

I have:

@MappedSuperClass
public class A{

   private int boo;

   @Column(name = boo)
   public int getBoo(){
       return boo;
   }

   public void setBoo(int boo){
       this.boo = boo;
   }  
}

The problem is, the column 'boo' may not exist for all tables. Is it possible to annotate getBoo() such that if the column does not exist, it keeps 'boo' as null?

Upvotes: 1

Views: 2124

Answers (3)

Sebastien Lorber
Sebastien Lorber

Reputation: 92200

At work we kinda have the same problem: an application that is used by 27 countries. All of these coutries want their own features, and so their own db columns.

In JPA/Hibernate it's not possible do to that. You have to share the same entity attributes for all countries, which means creation useless columns for many countries, and handle country by country the db constraints.

Upvotes: 2

tscho
tscho

Reputation: 2054

The clear answer is: No, you cannot do this. You should definitely reconsider your design. Why not use inheritance and move boo down to a derived class?

Btw: if you could set boo to null like you described it in your question, the int boo would explode with a NPE ;)

Upvotes: 0

korifey
korifey

Reputation: 3519

Maybe it's better to use inheritance here, I mean class AWithBoo extends A.

Upvotes: 4

Related Questions