使用Javax-RS& amp; POST复杂JSON Java Persistence& REST风格

时间:2014-08-22 14:30:57

标签: java json rest persistence

如果这是重复的话,我想提前道歉,但是,我在过去的2天里一直在寻找并且没有找到解决我问题的任何东西。

我已经创建了一个Web服务,我想要POST一个JSON对象。我的问题如下:

我们说我有三个对象。

ObjectA:{
"name":"",
"address":"",
"id":""
}

ObjectB:{
"id:"",
"name":"",
"objectA":{ [ObjectA]}
}

ObjectC:{
"id:"",
"name":"",
"objectA":{},
"objectB":{}
}

如您所见,ObjectC引用了ObjectA和ObjectB,它们也引用了ObjectA。插入新的ObjectC时,ObjectC.objectA应与ObjectC.objectB.objectA相同。

POST由以下方法使用:

@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(ObjectC entity) {        
            super.create(entity);
}

这些类看起来像这样:(加上getter和setter)

@Entity
@Table(name = "object_a")
public class ObjectA{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @NotNull
    @Column(name = "u_name")
    private String uName;

    @Column(name = "address")    
    private String address;
}

@Entity
@Table(name = "object_b")
public class ObjectB{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @JoinColumn(name = "object_a", referencedColumnName = "id")
    @ManyToOne(optional = false, cascade = CascadeType.PERSIST)
    @Valid
    private ObjectA objectA;
}

@Entity
@Table(name = "object_c")
public class ObjectC{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @JoinColumn(name = "object_a", referencedColumnName = "id")
    @ManyToOne(optional = false, cascade = CascadeType.PERSIST)
    @Valid
    private ObjectA objectA;

    @JoinColumn(name = "object_b", referencedColumnName = "id")
    @ManyToOne(optional = false, cascade = CascadeType.PERSIST)
    @Valid
    private ObjectB objectB;

}

注意:我可以毫无问题地发布ObjectA和ObjectB。

问题 问题是ObjectC.objectA和ObjectC.objectB.objectA作为不同的值被插入(或尝试),因为ObjectA.uName是唯一的,因此抛出异常。如果不是这样的话,那么就会创建两个新的ObjectA&#39。

通过执行以下操作,我能够在Hibernate中解决这个问题:

ObjectA a = new ObjectA();
ObjectB b = new ObjectB();
ObjectC c = new ObjectC();

b.setObjectA(a);
c.setObjectA(a);
c.setObjectB(b);

session.beginTransaction();
session.save(a);
session.save(b);
session.save(c);
session.getTransaction().commit();

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

对于那些可能遇到此问题的人,我找到了解决方案。

我的做法与我在hibernate中做的非常相似(参见上文)。

我编辑了上面发布的 create 方法,对此:

@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(ObjectC entity) {

    if(entity.getObjectA().getId()==null){//If objectA doesn't have an ID, it must be new

            entity.getObjectB().setObjectA(entity.getObjectA());
            em.persist(entity.getObjectA());
            em.persist(entity.getObjectB());
            em.persist(entity);     


    }else{//Otherwise, everything may be inserted at once.
        super.create(entity);        
    }

}

顺便说一句,super.create(entity)方法包含以下内容:

public void create(T entity){            
        getEntityManager().persist(entity);       

}