2017-05-19 11 views
5

में पैरेंट-आईडी प्राप्त करें मेरे पास दो इकाइयों के साथ एक बिडरेक्शनल रिलेशनशॉप (एक से कई, एक से एक) के साथ एक वसंत विश्राम अनुप्रयोग है। नेस्टेड फ़ेचिंग समस्याओं को दूर करने के लिए, @ जेसन मैनेज्ड रेफरेंस/@ जेसनबैक रेफरेंस का उपयोग संस्थाओं के बीच एक अनुवर्ती/बाल संबंध के लिए किया गया है।एक बिडरेक्शनल हाइबरनेट मैपिंग

entites इस रूप में देखने के लिए:

@Entity 
@Table(name = "Parent") 
@JsonInclude(JsonInclude.Include.NON_NULL) 
public class Parent implements java.io.Serializable { 

    private Integer id; 
    private List<Child> childList; 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "ID", unique = true, nullable = false) 
    public Integer getId() { 
     return this.id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) 
    @JsonManagedReference 
    public List<Child> getChildList() { 
     return childList; 
    } 

    public void setChildListe(List<Child> childListe) { 
      this.childList = childList; 
     } 

    } 


@Entity 
@Table(name = "Child") 
@JsonInclude(JsonInclude.Include.NON_NULL) 
public class Child implements java.io.Serializable { 


    private Integer id; 
    private Parent parent; 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "ID", unique = true, nullable = false) 
    public Integer getId() { 
     return this.id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "ParentID") 
    @JsonBackReference 
    public Parent getParent() { 
     return parent; 
    } 

    public void setParent(Parent parent) { 
     this.parent = parent; 
    } 


} 

यह ठीक काम करता है जब जनक तत्व प्राप्त करने में कठिनाई, childset तो साथ दिलवाया और एक json सरणी के रूप में प्रदर्शित है।

हालांकि, jsonbackreferance के उपयोग के कारण बच्चे तत्व में माता-पिता का कोई संदर्भ नहीं है।

मैं या तो जेसन प्रतिक्रिया में बच्चे-ग्यारह से जुड़ी मूल वस्तु को प्राप्त करना चाहता हूं, या कम से कम एक सिंगल बाल तत्व के लिए अनुरोध करते समय, कम से कम मूल आईडी प्राप्त करना चाहता हूं।

सभी प्रतिक्रिया appriciated जाएगा :)

+1

मैं भी है इस मुद्दे – esoni

+0

समस्या इस प्रकार का हल करने के लिए किया जा सकता है आमतौर पर आपके डिजाइन के साथ एक मुद्दा से उपजी है। यदि रिफैक्टरिंग का कोई मौका नहीं है तो आप पूरी तरह से बैक संदर्भ के बजाय बच्चे को स्पष्ट रूप से मूल आईडी जोड़ सकते हैं। –

उत्तर

1

मैं ने वही समस्या थी और मैं कस्टम serializer/deserializer

का उपयोग कर यह मैं कैसे procedeed है द्वारा इसे हल (कृपया इसे अपने कोड के लिए अनुकूल):

ChildSerializer

public class ChildSerializer extends StdSerializer<Child> { 

    private static final long serialVersionUID = -265706839304575646L; 
    public ChildSerializer(Class<Child> t) { 
     super(t); 
    } 
    public ChildSerializer() { 
     this(null); 
    } 
    @Override 
    public void serialize(Child child, JsonGenerator jg, SerializerProvider sp) 
      throws IOException, JsonGenerationException { 

     jg.writeStartObject(); 
     jg.writeStringField("name", child.getName()); 
     jg.writeStringField("surname", child.getSurname()); 
     Parent parent = child.getParent(); 
     jg.writeObjectFieldStart("parent"); 
     jg.writeStringField("name", parent.getName()); 
     jg.writeStringField("surname", parent.getSurname()); 
     jg.writeEndObject(); 
    } 
} 

बाल

@JsonSerialize(using = ChildSerializer.class) 
public class Child implements Serializable { 

    private static final long serialVersionUID = 7902561110976676934L; 
    private String name; 
    private String surname; 
    private Parent parent; 

    public Child(String name, String surname, Parent parent) { 
     this(name, surname); 
     this.parent = parent; 
    } 
    public Child(String name, String surname) { 
     this(); 
     this.name = name; 
     this.surname = surname; 
    } 
    public Child() { 
     super(); 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getSurname() { 
     return surname; 
    } 
    public void setSurname(String surname) { 
     this.surname = surname; 
    } 
    public Parent getParent() { 
     return parent; 
    } 
    public void setParent(Parent parent) { 
     this.parent = parent; 
    } 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((name == null) ? 0 : name.hashCode()); 
     result = prime * result + ((parent == null) ? 0 : parent.hashCode()); 
     result = prime * result + ((surname == null) ? 0 : surname.hashCode()); 
     return result; 
    } 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Child other = (Child) obj; 
     if (name == null) { 
      if (other.name != null) 
       return false; 
     } else if (!name.equals(other.name)) 
      return false; 
     if (parent == null) { 
      if (other.parent != null) 
       return false; 
     } else if (!parent.equals(other.parent)) 
      return false; 
     if (surname == null) { 
      if (other.surname != null) 
       return false; 
     } else if (!surname.equals(other.surname)) 
      return false; 
     return true; 
    } 
} 

जनक

public class Parent implements Serializable { 

    private static final long serialVersionUID = -5604725691780073247L; 
    private String name; 
    private String surname; 
    private List<Child> childs; 

    public Parent(String name, String surname) { 
     this(); 
     this.name = name; 
     this.surname = surname; 
    } 
    public Parent(String name, String surname, List<Child> childs) { 
     this(name, surname); 

     this.childs = childs; 
    } 
    public Parent() { 
     super(); 
     childs = new ArrayList<Child>(); 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getSurname() { 
     return surname; 
    } 
    public void setSurname(String surname) { 
     this.surname = surname; 
    } 
    public void addChild(Child child) 
    { 
     if(!childs.contains(child)) 
     { 
      child.setParent(this); 
      childs.add(child); 
     } 
    } 
    public List<Child> getChilds() { 
     return childs; 
    } 
    public void setChilds(List<Child> childs) { 
     this.childs = childs; 
    } 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((childs == null) ? 0 : childs.hashCode()); 
     result = prime * result + ((name == null) ? 0 : name.hashCode()); 
     result = prime * result + ((surname == null) ? 0 : surname.hashCode()); 
     return result; 
    } 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Parent other = (Parent) obj; 
     if (childs == null) { 
      if (other.childs != null) 
       return false; 
     } else if (!childs.equals(other.childs)) 
      return false; 
     if (name == null) { 
      if (other.name != null) 
       return false; 
     } else if (!name.equals(other.name)) 
      return false; 
     if (surname == null) { 
      if (other.surname != null) 
       return false; 
     } else if (!surname.equals(other.surname)) 
      return false; 
     return true; 
    } 

} 

मेरे परीक्षण विधि:

@Test 
public void testJson() 
{ 
    try 
    { 
     Parent p = new Parent(UUID.randomUUID().toString(), UUID.randomUUID().toString()); 
     for (int i = 0; i < 10; i++) { 
      p.addChild(new Child(UUID.randomUUID().toString(), UUID.randomUUID().toString())); 
     } 
     ObjectMapper om = new ObjectMapper(); 
     String childJson = om.writeValueAsString(p.getChilds().get(0)); 
     System.out.println(childJson); 
    } catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

अंतिम आउटपुट था:

{"name":"b86eab86-9858-4536-9c5c-d44d22810fc1","surname":"9a6249f0-58df-44e5-a1b9-31fbad6e9f49","parent":{"name":"74b0cd97-64a1-4547-ab22-4e4eedd0759b","surname":"a33c79f3-6f26-478b-9e24-7df96b3b1f68"}} 

दुर्भाग्य से एनोटेशन ar ई शक्तिशाली लेकिन हमेशा नहीं वे तुम्हें करने के लिए प्राप्त करने के लिए आपको क्या चाहिए अनुमति देते हैं और इसलिए आप "अपने आप से" यह क्या करना है

मुझे आशा है कि यह उपयोगी

संबंधित मुद्दे