JAXB parsing an xml file where properties are on a single line - java

I have this particular xml file(in that exact format) that I am trying to parse with JAXB
Because the properties are all on a single line it doesn't see them and returns all fields as null in my main function. How can I parse the xml correctly in it's format?
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee firstName="Asya" id="2" lastname="Olshansky"/>
</employees>
This is the code for employee
#XmlRootElement(name = "employee")
#XmlAccessorType (XmlAccessType.FIELD)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
public Employee() {
super();
}
//Setters and Getters
#Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
Employees code:
#XmlRootElement(name = "employees")
#XmlAccessorType(XmlAccessType.FIELD)
public class Employees {
#XmlElement(name = "employee")
List<Employee> employees = null;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> list) {
this.employees = list;
}
}
This is the Main execution:
public static void main(String[] args)
{
String fileName = "employee.xml";
jaxbXmlFileToObject(fileName);
}
private static void jaxbXmlFileToObject(String fileName) {
File xmlFile = new File(fileName);
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(Employees.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Employees employees = (Employees) jaxbUnmarshaller.unmarshal(xmlFile);
for(Employee e: employees.getEmployees() )
System.out.println(e);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}

Try:
Employee.java:
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
public Employee() {
super();
}
public Employee(Integer id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
#XmlAttribute(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#XmlAttribute(name="firstName")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#XmlAttribute(name="lastname")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
Employees.java:
#XmlRootElement(name="employees")
public class Employees {
List<Employee> employees;
public Employees() {}
public Employees(List<Employee> employees) {
super();
this.employees = employees;
}
#XmlElement(name="employee")
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> list) {
this.employees = list;
}
}
Output:
Note:
No changes in Main.
Updated Employee and Employees class.
Added #XmlAttribute in Employee to map attribute names and getter/setters.
Added #XmlElement in Employees to map each employee element inside the employees tag and constructor also.

Related

XML to java object using jaxb Unmarshalling namespace

I have below employee.xml which I am trying to convert to Java Object using JAXB. I am getting null value here, please guide me what I am missing here or doing wrong.
I am not familiar with JAXB and namespace <employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schema.cs.csg.com/cs/ib/cpm"> I thing I am missing some annotation or not using it properly.
Output:
Employee [id=null, firstName=null, lastName=null, department=null]
employee.xml
<?xml version="1.0"?>
<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlnx="http://schema.cs.csg.com/cs/ib/cpm">
<department>
<id>101</id>
<name>IT</name>
</department>
<firstName>Rakesh</firstName>
<id>1</id>
<lastName>Yadav</lastName>
</employee>
Employee.java
package com.cs.xmltojava
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "employee", namespace="http://schema.cs.csg.com/cs/ib/cpm")
#XmlAccessorType(XmlAccessType.PROPERTY)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
private Department department;
public Employee() {
super();
}
public Employee(int id, String fName, String lName, Department department) {
super();
this.id = id;
this.firstName = fName;
this.lastName = lName;
this.department = department;
}
//Setters and Getters
public Integer getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
#Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department="+ department + "]";
}
}
Department.java
package com.cs.xmltojava
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "department", namespace="http://schema.cs.csg.com/cs/ib/cpm")
#XmlAccessorType(XmlAccessType.PROPERTY)
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
Integer id;
String name;
public Department() {
super();
}
public Department(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
//Setters and Getters
public Integer getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}
}
EmployeeMain.java
package com.cs.xmltojava
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class EmployeeMain {
public static void main(String[] args) throws Exception {
File xmlFile = new File("employee.xml");
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println(employee);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
create package-info.java in your package and remove
namespace="http://schema.cs.csg.com/cs/ib/cpm" from #XmlRootElement.
package-info.java
#XmlSchema(
namespace="http://schema.cs.csg.com/cs/ib/cpm",
elementFormDefault=XmlNsForm.QUALIFIED
)
package com.cs.xmlparser;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

Produce JSON with name of the object from REST web services in Java

I wrote a REST web service which is returning JSON as below
[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"},
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]
But I want the output with the "student" appended as below.
{"student":[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"},
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]}
how can I achieve this output?
Below is the Product Class
#XmlRootElement(name="student")
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public Student() {
super();
}
public Student(int id, String name, String age, String dob, String phone,
String sslc, String hsc, String college) {
super();
this.id = id;
this.name = name;
this.age = age;
this.dob = dob;
this.phone = phone;
this.sslc = sslc;
this.hsc = hsc;
this.college = college;
}
private int id;
private String name;
private String age;
private String dob;
private String phone;
private String sslc;
private String hsc;
private String college;
#XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#XmlElement
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
#XmlElement
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
#XmlElement
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
#XmlElement
public String getSslc() {
return sslc;
}
public void setSslc(String sslc) {
this.sslc = sslc;
}
#XmlElement
public String getHsc() {
return hsc;
}
public void setHsc(String hsc) {
this.hsc = hsc;
}
#XmlElement
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
#Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", dob=" + dob + ", phone=" + phone + ", sslc=" + sslc
+ ", hsc=" + hsc + ", college=" + college + "]";
}
}
Below is the service class.
#GET
#Path("/student.srv")
#Produces("application/json")
public Response getStudentJson(){
DAOLayer daoLayer=new DAOLayer();
List<Student> studentsList=null;
try {
studentsList=daoLayer.getStudents();
} catch (SQLException e) {
e.printStackTrace();
}
return Response.ok(studentsList).build();
}
Kindly help me to achieve the above mentioned output.
Thanks in Advance.
To get the desired output, you will have to create one single root object containing a List<Student> student and return it:
Root.java
#XmlRootElement(name="root")
public class Root implements Serializable {
#XmlList
private List<Student> student = new ArrayList<Student>();
// getter and setter
}
Service.java
#GET
#Path("/student.srv")
#Produces("application/json")
public Response getStudentJson(){
DAOLayer daoLayer=new DAOLayer();
List<Student> studentsList=null;
try {
studentsList=daoLayer.getStudents();
} catch (SQLException e) {
e.printStackTrace();
}
Root root = new Root();
root.setStudent(studentsList),
return Response.ok(root).build();
}

How to convert Json to Java Object, Deserializing Json

data_user = "{"id":1,"lastName":"lastName","name":"name","school":{"id":1}}"
public class School {
private int id;
private String name;
}
public class User {
private int id;
private String lastName;
private String name;
private School school;
}
How to deserialize Json data_user to java object User?
I tried with Gson :
Gson gson = new Gson();
User user = gson.fromJson(data_user, User.class)
But I have an error with this code because the Json contains a school which hasn't the school's name.
How Can I serialize the Json to java Object?
School.java
public class School {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "School [id=" + id + ", name=" + name + "]";
}
}
User.java
public class User {
private int id;
private String lastName;
private String name;
private School school;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
#Override
public String toString() {
return "User [id=" + id + ", lastName=" + lastName + ", name=" + name
+ ", school=" + school + "]";
}
}
Main.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testgson.beans.User;
public class Main {
private static Gson gson;
static {
gson = new GsonBuilder().create();
}
public static void main(String[] args) {
String j = "{\"id\":1,\"lastName\":\"lastName\",\"name\":\"ignacio\",\"school\":{\"id\":1}}";
User u = gson.fromJson(j, User.class);
System.out.println(u);
}
}
Result
User [id=1, lastName=lastName, name=ignacio, school=School [id=1, name=null]]
Try with the Jackson Library. With Gson with should have not any problem, I tried with the code of #Saurabh and it work well

JPA Entity being ignored

I'm trying to use JPA for the first time in a project. Most of my entities are working fine, but I am having trouble with one which is part of a Joined Inheritance Strategy.The entities are also being serialised by Jackson so they also have Json annotations.
The parent "User" class:
(Edit: added "Type" field)
#JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.WRAPPER_OBJECT)
#JsonTypeName("user")
#JsonSubTypes({
#JsonSubTypes.Type(name="customer", value=Customer.class),
#JsonSubTypes.Type(name="employee", value=Employee.class)})
#Entity(name = "User")
#Table(name="user")
#Inheritance(strategy = InheritanceType.JOINED)
#DiscriminatorColumn(name="type",discriminatorType = DiscriminatorType.INTEGER)
#NamedQuery(name="User.all",query = "select u from User u")
public abstract class User {
#Id
private String username;
#Column(name = "type",nullable = false)
private int type;
public User(){
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public abstract Set<Order> getOrders();
}
A Child "Employee"
#JsonTypeName("employee")
#Entity(name="Employee")
#Table(name="employee")
#PrimaryKeyJoinColumn(name = "username",referencedColumnName = "username")
#DiscriminatorValue("1")
#NamedQuery(name = "Employee.all",query = "select e from Employee e")
public class Employee extends User implements Serializable{
private String username;
private String firstName;
private String lastName;
private String email;
#Convert(converter = LocalDatePersistenceConverter.class)
private LocalDate dateStarted;
#Convert(converter = LocalDatePersistenceConverter.class)
private LocalDate dateEnded;
#OneToMany(mappedBy = "employee",targetEntity = Order.class,fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
#JsonIgnore
private Set<Order> orders = new HashSet<>();
public Employee() {
}
#Override
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public void addOrder(Order order){
orders.add(order);
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getDateStarted() {
if(dateStarted != null)
return dateStarted.toString();
else return null;
}
public void setDateStarted(LocalDate dateStarted) {
this.dateStarted = dateStarted;
}
public String getDateEnded() {
if(dateEnded != null)
return dateEnded.toString();
else return null;
}
public void setDateEnded(LocalDate dateEnded) {
this.dateEnded = dateEnded;
}
#Override
public String toString(){
return getUsername();
}
}
And a child "Customer":
(Edit: removed #Id field)
#JsonTypeName("customer")
#Entity(name="Customer")
#Table(name="customer")
#PrimaryKeyJoinColumn(name = "username",referencedColumnName = "username")
#DiscriminatorValue("2")
#NamedQueries({
#NamedQuery(name="Customer.all",query = "select c from Customer c")
})
public class Customer extends User implements Serializable{
public enum VIP_TYPE {NORMAL,SILVER,GOLD,DIAMOND}
#Transient
private static final int SILVER_THRESHOLD = 1000;
#Transient
private static final int GOLD_THRESHOLD = 2000;
#Transient
private static final int DIAMOND_THRESHOLD = 3000;
private String firstName;
private String lastName;
private String email;
private String address;
private String postcode;
private String mobileNumber;
private String homeNumber;
#Convert(converter = VipTypeConverter.class)
private VIP_TYPE vipGroup;
private String discount;
#OneToMany(mappedBy = "customer",targetEntity = Order.class,fetch=FetchType.EAGER,cascade = CascadeType.ALL)
#JsonIgnore
private Set<Order> orders = new HashSet<>();
public Customer() {
}
#Override
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public void addOrder(final Order order){
orders.add(order);
updateVipGroup();
}
private void updateVipGroup() {
int sum = orders.stream().map(Order::getPayment).distinct().mapToInt(p->p.getAmmount()).sum();
if(sum > DIAMOND_THRESHOLD){
vipGroup = VIP_TYPE.DIAMOND;
return;
}
if(sum > GOLD_THRESHOLD){
vipGroup = VIP_TYPE.GOLD;
return;
}
if(sum > SILVER_THRESHOLD){
vipGroup = VIP_TYPE.SILVER;
return;
}
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public void setAddress(String address) {
this.address = address;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public void setVipGroup(VIP_TYPE vipGroup) {
this.vipGroup = vipGroup;
}
public void setHomeNumber(String homeNumber) {
this.homeNumber = homeNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getDiscount() {
return discount;
}
public VIP_TYPE getVipGroup() {
return vipGroup;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getAddress() {
return address;
}
public String getPostcode() {
return postcode;
}
public String getMobileNumber() {
return mobileNumber;
}
public String getHomeNumber() {
return homeNumber;
}
}
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="local" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/cod</jta-data-source>
<class>com.technicalpioneers.cod.user.Customer</class>
<class>com.technicalpioneers.cod.user.Employee</class>
<class>com.technicalpioneers.cod.user.User</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>
Everything to do with "employee" works file, I can use the named query Employee.all to find all the employees in the database.
However, If I try to retrieve any customers I get errors. If I try to run the named query Customer.all I get:
java.lang.IllegalArgumentException: NamedQuery of name: Customer.all not found.
If I try to use EntityManager's find() method to find a particular customer I get:
javax.servlet.ServletException: Exception [EclipseLink-43] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class for indicator field value [2] of type [class java.lang.Integer].
Descriptor: RelationalDescriptor(com.technicalpioneers.cod.user.User --> [DatabaseTable(user)])
I don't understand why the Customer entity is not being found by JPA. I've checked the user table and the "type" column is there with correct numbers, and #DescriminatorValue is set correctly. It's almost like the annotations are being ignored?
Have done many clean rebuilds and redeploys too. Any help would be very much appreciated!
I found this eventually. https://bugs.eclipse.org/bugs/show_bug.cgi?id=429992
It turns out EclipseLink will silently ignore entities with lambda expressions! Very annoying for it to not be at least mentioned in logs!
Thanks to everyone who took the time!

Not able to save ManyToMany self-reference relationship in hibernate

I have a ManyToMany relationship between person that I'm trying to describe with Hibernate annotations. I have also created a test for this but the problem is that the relationship isn't saved. Please help me find where I did wrong!
Entity:
#Entity(name = "person")
#Table(appliesTo = "person", indexes = {
#org.hibernate.annotations.Index(name = "ix_uuid", columnNames = {"uuid"}),
#org.hibernate.annotations.Index(name = "ix_facebookId", columnNames = {"facebookId"})
})
public class Person implements Serializable {
#Id
#GeneratedValue(strategy = AUTO)
private Long id;
private String uuid;
private String firstName;
private String lastName;
private String facebookId;
private String email;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(
name = "person_friend",
joinColumns = #JoinColumn(name = "person_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "friend_id", referencedColumnName = "id")
)
private Set<Person> persons = new HashSet<Person>();
#ManyToMany(mappedBy = "persons", cascade = CascadeType.ALL)
private Set<Person> friends = new HashSet<Person>();
public Person(String uuid, String firstName, String lastName, String facebookId, String email) {
this.uuid = uuid;
this.firstName = firstName;
this.lastName = lastName;
this.facebookId = facebookId;
this.email = email;
}
public Person() {
// Hibernate
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFacebookId() {
return facebookId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void addFriend(Person person){
if(!getFriends().contains(person)){
getFriends().add(person);
person.getPersons().add(this);
}
}
public void becomeFriendOf(Person person) {
if(!getPersons().contains(person)){
getPersons().add(person);
person.getFriends().add(this);
}
}
public Set<Person> getFriends() {
return friends;
}
public void setFriends(Set<Person> friends) {
this.friends = friends;
}
public Set<Person> getPersons() {
return persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
#Override
public String toString() {
return "Person{" +
"id=" + id +
", uuid='" + uuid + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", facebookId='" + facebookId + '\'' +
", email='" + email + '\'' +
'}';
}
}
PersonDao:
#Repository("personDao")
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
#Autowired
public PersonDaoImpl(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
#Override
public void save(Person person) {
getSession().save(person);
}
#Override
public Person getPerson(Long id) {
return (Person) getSession().get(Person.class, id);
}
#Override
public void saveFriendship(Friendship friendship) {
getSession().save(friendship);
}
}
Test:
#Test(groups = {"integration"})
#ContextConfiguration(locations = {"classpath:applicationContext-test.xml" })
public class PersonDaoImplTest extends AbstractTestNGSpringContextTests {
#Autowired
private PersonDao target;
#Test
public void loadDatabaseWithSomeInitialValues(){
System.out.println(applicationContext.toString());
Person person = new Person("12345abcde","test","test2","test.test", "test.test#gmail.com");
Person person2 = new Person("4567abcde","fest","fest","fest.fest", "fest.fest#gmail.com");
Person person3 = new Person("89105abcde","best","best","best.best", "best.best#gmail.com");
person.addFriend(person2);
person.addFriend(person3);
person2.becomeFriendOf(person);
person3.becomeFriendOf(person);
target.save(person);
}
}
}
As stated the persons are saved in the person table but not the relationship in the person_friend table. Why?
Perhaps it has something to do with transaction boundaries. Try to make your test method #Transactional.

Categories

Resources