消除双向递归关系的最简单方法

消除双向递归关系的最简单方法

本文介绍了消除双向递归关系的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Gson 库将 Java 对象转换为 Json 响应..问题是在 JPA 请求后,由于与其他实体的递归关系,无法转换从 DB 检索的对象(参见我之前的问题) 例如:

I use the Gson library to convert Java objects to a Json response...the problem is that after a JPA requests the object retrieved from DB can not be converted because of a recursive relationship with other entities(see my previous question) for example :

public class Gps implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "IMEI", nullable = false, length = 20)
    private String imei;
    //some code here...
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "gpsImei", fetch = FetchType.LAZY)
    private List<Coordonnees> coordonneesList;


public class Coordonnees implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "IDCOORDONNEES", nullable = false)
    private Integer idcoordonnees;
    //some code here...
    @JoinColumn(name = "GPS_IMEI", referencedColumnName = "IMEI", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Gps gpsImei;

我的源代码:

  EntityManagerFactory emf=Persistence.createEntityManagerFactory("JavaApplication21PU");
  GpsJpaController gjc=new GpsJpaController(emf);

  Gps gps=gjc.findGps("123456789012345");

  for(int i=0;i<gps.getCoordonneesList().size();i++){
   gps.getCoordonneesList().get(i).setGpsImei(null);
  }

  Gson gson=new Gson();
  String json=gson.toJson(gps);//convert to json response

  System.out.println(json);

正如你在这里看到的,我做了:

As you can see here i made :

   for(int i=0;i<gps.getCoordonneesList().size();i++){
     gps.getCoordonneesList().get(i).setGpsImei(null);
   }

只是通过为 coordonneesList 中的每个 GPS 对象设置 null 来终止递归关系..

only to kill the recursive relationship by setting null for each GPS object in the coordonneesList..

您认为这是一个很好的解决方案,还是有其他更实用的方法?谢谢

In your opinion this is a good solution or is there another method more practical?Thanks

推荐答案

有一个 Gson 扩展名为 GraphAdapterBuilder 可以序列化包含循环引用的对象.这是来自相应测试用例的一个非常简化的示例:

There's a Gson extension called GraphAdapterBuilder that can serialize objects that contain circular references. Here's a very simplified example from the corresponding test case:

Roshambo rock = new Roshambo("ROCK");
Roshambo scissors = new Roshambo("SCISSORS");
Roshambo paper = new Roshambo("PAPER");
rock.beats = scissors;
scissors.beats = paper;
paper.beats = rock;

GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder()
    .addType(Roshambo.class)
    .registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
System.out.println(gson.toJson(rock));

打印:

{
  '0x1': {'name': 'ROCK', 'beats': '0x2'},
  '0x2': {'name': 'SCISSORS', 'beats': '0x3'},
  '0x3': {'name': 'PAPER', 'beats': '0x1'}
}

请注意,GraphAdapterBuilder 类包含在 gson.jar 中.如果你想使用它,你必须手动将它复制到你的项目中.

Note that the GraphAdapterBuilder class is not included in gson.jar. If you want to use it, you'll have to copy it into your project manually.

这篇关于消除双向递归关系的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:22