Ids和复杂数据映射REST&过冬 [英] Ids and Complex Data Mapping over REST & Hibernate

查看:147
本文介绍了Ids和复杂数据映射REST&过冬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些相互关联的非常复杂的数据对象,我需要生活在两个独立的数据库(两个应用程序)中。数据的存储不用担心,并且这两个应用程序( source target )都在查看同样的基础类数据来处理setter,getters,hibernate等。

因为我想设计一个REST服务是非常项目中心的,所以我有几个调用批量设置对象。在加载完成后,我正在考虑根据项目的ID来调用项目特定的REST调用GET信息。这引出了一些问题,我不确定REST是否可以处理。



如果源代码应用程序设置时,我担心基于Hibernate中ID的项目关联 @JoinTable ,当它通过对目标。尝试使用目标中的ID来获取<$ c $>项目/ {id} / relationships c> {id} 来自目标,并且与数据之后的不匹配



在REST或Hibernate中是否有处理从移动大量复杂定制关系数据的方法$ c>到目标系统使用REST调用,基于ID的URL,没有冲突或问题?

示例类和数据库(代码简化了很多以忽略一些复杂性):

  @Table(name =GUIDELINE)
公共课程准则{
私人字符串名称;
私人套餐< RelatedItem> relatedItems;
保护长ID;
/ ** Hibernate使用的这个对象的数据库ID。 * /
@Id
@GeneratedValue(generator =increment)
@GenericGenerator(name =increment,strategy =increment)
@XmlElement
@Column(name =ID,unique = true,nullable = false)
public Long getId(){
return id;
}

@ManyToMany(fetch = FetchType.EAGER)
@JsonIgnore
@JoinTable(name =RELATIONSHIPS,
joinColumns = {@JoinColumn (name =GUIDELINE_ID,referencedColumnName =ID,table =GUIDELINE,nullable = false)},
inverseJoinColumns = {@JoinColumn(name =ITEM_ID,referencedColumnName =ID,table = RELATEDITEM,nullable = false)})
public Set< RelatedItem> getRelatedItems(){
return relatedItems;


@Column(name =NAME,unique = true,nullable = false,length = 240)
public String getName(){
return this 。名称;



@Table(name =RELATEDITEM)
public class RelatedItem {
private String name;
保护长ID;
/ ** Hibernate使用的这个对象的数据库ID。 * /
@Id
@GeneratedValue(generator =increment)
@GenericGenerator(name =increment,strategy =increment)
@XmlElement
@Column(name =ID,unique = true,nullable = false)
public Long getId(){
return id;


@Column(name =NAME,unique = true,nullable = false,length = 240)
public String getName(){
return this 。名称;
}
}


解决方案

常规方法是使用自然键来识别分布式环境中的对象。除了生成的密钥外,您还可以添加自然键或使用生成器为主键创建UUID值。


I have some pretty complex data objects which relate to each other, that I need to have live in two separate databases (two applications). The storage of the data is not a worry, and both of the applications (source and target) are looking at the same base class of data to handle setters, getters, hibernate, etc.

Since I want to design a REST service to be very item centric, I have a few calls that setup the objects in bulk. After those are loaded in, I was thinking of going through and calling an item specific REST call to GET information based on the item's ID. This brings up a few issues I am not sure if REST can handle.

If the ID on the source application is set, I am worried about item associations based on ID in Hibernate @JoinTable when it ports over through a REST call to the target. Trying to use the ID from target to GET item/{id}/relationships yields issues since the {id} is from the target and doesn't match source after data has been loaded.

Is there a way in REST or Hibernate to handle moving large sets of complex custom relational data from a source to a target system using REST calls, ID based URLs, without collisions or issues?

Example Classes and DB (code simplified a lot to ignore some complexity):

@Table(name="GUIDELINE")
public class Guideline{
  private String name;
  private Set<RelatedItem> relatedItems;
  protected Long id;
  /** Database id of this object used by Hibernate. */
  @Id
  @GeneratedValue(generator="increment")
  @GenericGenerator(name="increment", strategy = "increment")
  @XmlElement
  @Column(name = "ID", unique = true, nullable = false)
  public Long getId() {
    return id;
  }

  @ManyToMany(fetch = FetchType.EAGER)
  @JsonIgnore
  @JoinTable(name = "RELATIONSHIPS", 
  joinColumns = { @JoinColumn(name = "GUIDELINE_ID", referencedColumnName = "ID", table = "GUIDELINE", nullable = false) }, 
  inverseJoinColumns = { @JoinColumn(name = "ITEM_ID", referencedColumnName = "ID", table = "RELATEDITEM", nullable = false) })
  public Set<RelatedItem> getRelatedItems() {
    return relatedItems;
  }

  @Column(name = "NAME", unique = true, nullable = false, length = 240)
  public String getName(){
    return this.name;
  }
}

@Table(name="RELATEDITEM")
public class RelatedItem{
  private String name;
  protected Long id;
  /** Database id of this object used by Hibernate. */
  @Id
  @GeneratedValue(generator="increment")
  @GenericGenerator(name="increment", strategy = "increment")
  @XmlElement
  @Column(name = "ID", unique = true, nullable = false)
  public Long getId() {
    return id;
  }

  @Column(name = "NAME", unique = true, nullable = false, length = 240)
  public String getName(){
    return this.name;
  }
}

解决方案

General approach is to use natural key to identify objects in distributed environment. You can add natural key in addition to generated key or use generator to create UUID values for primary keys.

这篇关于Ids和复杂数据映射REST&amp;过冬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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