I know this question was asked before, but still no response to that.
Indeed, I have this Java Entity:
#Entity
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String email;
private String firstName;
private String lastName;
}
and my goal is to transform/map the class below into this xml format:
<customer>
<id>...</id>
<email>....</email>
<names>
<firstName>...</firstName>
<lastName>...</lastName>
<names>
</customer>
The problem is that I can't use #XmlElementWrapper because it is not applicable on non collection property, and I'm searching for a solution that doesn't require to create a class "names" which will contains firstname and lastname.
Related
I have two repositories - User and Address.
User has a one to one relationship with Address and they are linked by ID. See below code snippets. When I pull data using a JPA repository, I get basically a constant loop of data.
EG:
<Data>
<User>
<id>1</id>
<name>Mary</name>
<dob>21/01/1990</dob>
<Address>
<id>1<id>
<address>123 Main Street</address>
<User>
<id>1</id>
<name>Mary</name>
<dob>21/01/1990</dob>
<Address>
<id>1<id>
<address>123 Main Street</address>
<User>
....
and so on like this causing my query to take a large amount of time to run. Is there anyway to stop the User object from being returned within the Address object? Any help would be greatly appreciated.
#Entity
#Data
#Table(name = "ADDRESS")
public class Address{
#Id
#Column()
private String id;
#Column()
private String address;
#OneToOne (fetch = FetchType.LAZY)
#JoinColumn(name = "id")
private User user;
}
#Entity
#Data
#Table(name = "User")
public class User{
#Id
#Column()
private String id;
#Column()
private String name;
#Column()
private String dob;
#OneToOne ()
#JoinColumn(name = "id")
private Address address;
}
This is normal behaviour. Serializers call getters to serialize data which are intercepted by the Hibernate Proxy loading the data even if they are lazy. To prevent this, You have to add #JsonBackReference to the address field in your User class, and #JsonManagedReference to the user field in your Address class.
I am new to Spring services. I want to create a table in database which has one column with data type List.
#Entity
#Table(name = "employees")
public class Employees {
#Id
#Column(name= Id)
private long id;
#Column(name= Name)
private String name;
#Column(name= Skills)
private List<String> skills;
//getter setters for all
}
But on run I am getting error Could not determine type for: java.util.List for List
How can I resolve it? (I tried oneToMany annotation but I think I did not use it in right way)
I have defined two JPARepository with spring: "Person" and "Address". I also specified a relation between Person and Address.
I can fetch all persons with:
http://localhost:8080/person/
and all address with: http://localhost:8080/address/
Also I can get the address of a single person with http://localhost:8080/person/1/address
Now I'd like to get the address to every person as a nested address when I get request: http://localhost:8080/person/
How can I include the relations in the response?
My classes as requested by #nicolasl
#Entity
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
#OneToOne
#JoinColumn(name = "address_id")
private Address address;
//Getter/Setter here
}
#Entity
public class Address {
#Id
#GeneratedValue
private long id;
#Column(nullable = false)
private String location;
#OneToOne(mappedBy = "address")
private Person person;
//Getter/Setter here
}
Well, I guess you just can't, not using the automated rest engine provided by spring. The rest API will return links to retrieve the related entities in the "_links" section of your JSON response (you can read more about that in http://stateless.co/hal_specification.html). You'd have to call those links to retrieve your Addresses.
Another way to solve this, is implementing your own repository and assembling your own JSON response.
I have a Class whose objects I want to be able to persist via Hibernate such as:
package my.package
#Entity
#Table(name="hibernatedclass")
public class HibernatedClass {
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
#Column(name="firstname")
private String firstname;
[....]
}
As it can be seen the name I have given the table to store the objects of my.package.HibernatedClass is named hibernatedclass.
What I am looking for in an answer is: How can this naming be automatized?
Something that would imho look similar to this pseudo-code.
#Table(name=class.getCanonicalName())
is there a simple way to persist some of the fields in another class and table using hibernate.
For example, I have a Person class with name, surname, email, address1, address2, city, country fields. I want my classes to be:
public class Person
{
private String name;
private String surname;
private String email;
private Address address;
// ..
}
public class Address
{
private Person person; // to whom this belongs
private String address1;
private String address2;
private String city;
private String country;
// ..
}
and I want to store Address in another table. What is the best way to achieve this?
Edit: I am using annotations. It does not have to be the way I described, I am looking for best practices.
Edit 2: What will be the Id of Address?
PS. If there is a way to make Address immutable (to use as a value object) that is even better, or maybe not because I thought everything from wrong perspective :)
map Address as an entity and add a primary key (an auto-generated id)
map the relation between Person and Address as one-to-one (#OneToOne on each field)
With Hibernate 3.5 it is possible to define foreign generators (aka. JPA mapping), details are here.
It is pretty straight forward Person should implement Serializable then #Id annotation is added to person.
#Entity
#AccessType(value = "field")
#Table(name = "addresses")
public class Address
{
#Id
#OneToOne
#JoinColumn(name = "person_id")
private Person person;
// ...
}
There is an alternative but I really like the first one:
#Entity
#AccessType(value = "field")
#Table(name = "addresses")
public class Address
{
#Id
private int personId;
#MapsId
#OneToOne
#JoinColumn(name = "person_id")
private Person person;
// ...
}