I have been following an excellent guide for generating pojos from a mysql database using hibernate. One can find the guide here for reference: Generate pojos with hibernate
I am getting pojos which have fields that embed other objects when a foreign key was present. For example, user's have addresses. Hibernate is generating something like the following:
public class User(){
private String name;
private Integer uid;
private Address address;
}
I have a problem, though, in that I want the classes to actually contain the foreign key value. For example, I want the User object to have a class field corresponding to the database field for addressId. So, I want the object to actually look something like this:
public class User(){
private String name;
private Integer uid;
private Integer addressId;
private Address address;
}
Does anyone know how to modify the hibernate code generation process so as to include foreign key values as fields on the object?
Update: I found a SO post which describes how to ignore the foreign key relationships and just get foreign keys as class fields: How to ignore foreign keys?
The problem here is that I want both. I don't want to ignore the relationships. I want them represented, but I also want the actual foreign key values.
Update:
Let me be more specific as to why I want this solution. We are trying to serialize these hibernate objects. Now, we have a lot of different hibernate pojos which are being reverse engineered. We do not want to manually write a serialization routine for every class. We would have to do that if we followed the convention of "just manually write an access method to the foreign key field on the embedded object". Further, even if we were to do so, the pojo still doesn't know what the field of the foreign key is called. Instead, we are using gson with a type adaptor.
With gson, we are serializing all fields on the pojo and just ignoring fields that contain a hibernate object. The problem, of course, is that we don't have the foreign key fields. We need a few pieces of information here in order to generically serialize any hibernate pojo. We need to know:
- The foreign key field name
- The foreign key field value