Eclipse hibernate pojo生成包含外键 [英] Eclipse hibernate pojo generation include foreign keys

查看:191
本文介绍了Eclipse hibernate pojo生成包含外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用hibernate从mysql数据库生成pojos的一个很好的指南。可以在这里找到指南以供参考:
使用hibernate生成pojos



我是当外号存在时,获取具有嵌入其他对象的字段的pojos。例如,用户有地址。 Hibernate生成如下的东西:

  public class User(){
private String name;
private Integer uid;
私人地址;
}

我有一个问题,但是,我希望类实际包含外键值。例如,我希望User对象具有与addressId的数据库字段对应的类字段。所以,我希望对象实际上看起来像这样:

  public class User(){
private String name ;
private Integer uid;
private整数addressId;
私人地址;有没有人知道如何修改hibernate代码生成过程,以便包括国外的代码。关键值作为对象上的字段?



更新:
我发现一个SO帖子描述了如何忽略外键关系并且只需获得外键作为课程字段:如何忽略外键?



这里的问题是我想要两个。我不想忽视关系。我想要他们代表,但我也想要实际的外键值。



更新:


$ b $让我更具体的是为什么我想要这个解决方案。我们正在尝试序列化这些hibernate对象。现在,我们有很多不同的hibernate pojos正在被逆向设计。我们不想为每个类手动编写一个序列化例程。如果我们遵循只是手动将访问方法写入嵌入对象的外键字段的约定,我们将不得不这样做。此外,即使我们这样做,pojo仍然不知道外键的领域是什么。相反,我们使用带有适配器的gson。



使用gson,我们正在序列化pojo上的所有字段,只是忽略包含hibernate对象的字段。问题当然是我们没有外键字段。我们在这里需要几条信息,以便通用序列化任何hibernate pojo。我们需要知道:


  1. 外键字段名称

  2. 外键字段值


解决方案

您的方法违反了Hibernate约定。因为Hibernate使用反射,所以约定对于Hibernate来说是很重要的。正因为如此,我怀疑马纪的遵循惯例的做法是最简单的。但是,如果它是不可协商的,您有两个选项可用。



您的第一个选项是添加一个暂时的getter,以显示getAddressId()函数。

  public class User(){
private String name;
private Integer uid;
私人地址;

// Getters,setters ...

@Transient
public boolean getAddressId(){
address.getId();
}
}

您的第二个选项是将数据访问层添加到在Hibernate对象之上强加自己的约定。这个抽象层不会受到Hibernate的约定的约束。这样,您的POJO将被DAO包装(数据访问对象),您可以设计



更新:



鉴于您的独特案例,请考虑修改序列化步骤GSON通常不能使用瞬态方法,但是有一个扩展可以做这个,作为显示此处



另一个解决方案是使用反射以您想要的方式复制对象,然后使用GSON序列化复制的对象。


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:

  1. The foreign key field name
  2. The foreign key field value

解决方案

Your approach violates Hibernate convention. Because Hibernate uses reflection, convention is essential for Hibernate to do it's job. Because of this, I suspect Maouven's "follow the convention" approach is easiest. However, if it is non-negotiable, you have two options available.

Your first option is to add a transient getter, to expose the getAddressId() function.

public class User() {
    private String name;
    private Integer uid;
    private Address address;

    // Getters, setters...

    @Transient
    public boolean getAddressId() {
        address.getId();
    }
}

Your second option is to add a Data Access layer to impose your own conventions on top of Hibernate objects. This layer of abstraction will not be bound by Hibernate's conventions. This way, your POJOs will be wrapped by DAOs (Data Access Objects), which you can design as you see fit.

Update:

Given your unique case, consider modifying your serialization step. GSON normally can't use transient methods, but there is an extension that can do this, as shown here.

Another solution would be to use reflection to copy the object the way you want it, and then use GSON to serialize the copied object.

这篇关于Eclipse hibernate pojo生成包含外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆