by

Card class - Integration of java JPA and JAXB API

The Card class contains a list of Port objects, using OneToMany annotation for its relation.
Many Card objects may refer to the same Equipment, with annotation ManyToOne and XmlIDREF.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/43630/Card-class---Integration-of-java-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.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) @Entity @Table(name = "CARD") public class Card implements Serializable { @XmlElement(name = "id") @XmlID @Id @GeneratedValue(strategy = GenerationType.AUTO) private String id; @XmlElement(name = "label") @Column(name = "LABEL", length = 125) private String label; @XmlElementRef @XmlElementWrapper(name = "ports") @OneToMany(mappedBy = "card", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) private List<Port> ports; @ManyToOne @XmlIDREF private Equipment equipment; public Card() { } public Card(String label) { this.label = label; } public String getId() { return id; } public void setId(String id) { this.id = id; } public void addPort(Port port) { if (ports == null) { this.ports = new ArrayList<Port>(); } port.setCard(this); ports.add(port); } public List<Port> getPorts() { return ports; } public void setPorts(List<Port> ports) { this.ports = ports; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public Equipment getEquipment() { return equipment; } public void setEquipment(Equipment equipment) { this.equipment = equipment; } @Override public String toString() { return "Card{" + "id=" + id + ", label=" + label + ", ports=" + ports + ", equipment=" + equipment + '}'; } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus