by
1
6
2,159
6
Top 1% !
Popular
Famous
Tagged
Specified
OpenSource
Popularity: 9264th place

Published on:

ClassNameNetwork
LanguageJava
LicenseMIT_X11
MethodNamegetEquipments
OutputTypeList<Equipment>

Network class - How to integrate JPA and JAXB java objects

Network class contains a list of Equipment objects. Note the OneToMany annotation, used to mark the relationship link between the two classes. 
FetchType EAGER is set to load all the structures once the object is referenced.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/43631/Network-class---How-to-integrate-JPA-and?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
package com.examples.jpa; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlID; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @Entity @XmlRootElement(name = "network-topology") @XmlType(propOrder={"equipments", "id"}) @XmlAccessorType(XmlAccessType.FIELD) public class Network { @XmlElement(name = "id") @XmlID @Id @GeneratedValue(strategy = GenerationType.AUTO) private String id; @XmlElementWrapper(name = "equipments-list") @XmlElement(name = "equipment") @OneToMany(mappedBy = "network", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) private List<Equipment> equipments; /** * @return the equipments */ public List<Equipment> getEquipments() { return equipments; } /** * @param equipments the equipments to set */ public void setEquipments(List<Equipment> equipments) { this.equipments = equipments; } /** * @param eqp the equipment to add */ public void addEquipments(Equipment eqp) { if (equipments == null) { this.equipments = new ArrayList(); } eqp.setNetwork(this);; equipments.add(eqp); } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Network [id=" + id + ", equipments=" + equipments + "]"; } /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus