Why should I save both objects if the address is nested in User?

I'm trying to get to know JPA better, so I created a very simple project. I have a user class and an address class. It seems like I have to persist, even if I add the address to my User class?

User:

import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "usr") // @Table is optional, but "user" is a keyword in many SQL variants 
@NamedQuery(name = "User.findByName", query = "select u from User u where u.name = :name")
public class User {
    @Id // @Id indicates that this it a unique primary key
    @GeneratedValue // @GeneratedValue indicates that value is automatically generated by the server
    private Long id;

    @Column(length = 32, unique = true)
    // the optional @Column allows us makes sure that the name is limited to a suitable size and is unique
    private String name;

    // note that no setter for ID is provided, Hibernate will generate the ID for us

    @OneToMany(fetch=FetchType.LAZY, mappedBy="user")
    private List<Address> addresses;

The address:

@Entity
public class Address {

    @Id // @Id indicates that this it a unique primary key
    @GeneratedValue // @GeneratedValue indicates that value is automatically generated by the server
    private Long id;

    @Column(length=50)
    private String address1;

    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;

EntityManager

EntityManager entityManager = Persistence.createEntityManagerFactory("tutorialPU").createEntityManager();

entityManager.getTransaction().begin();

User user = new User();
user.setName("User");

List<Address> addresses = new ArrayList<Address>();
Address address = new Address();
address.setAddress1("Address1");

addresses.add(address);
user.setAddresses(addresses);
entityManager.persist(user);
entityManager.persist(address);
entityManager.getTransaction().commit();
entityManager.close();

Maybe something is wrong ... just not sure what it is?

Any suggestions would be appreciated.

Thank,

S

+5
source share
3 answers

Try the item cascadefor annotation.

@OneToMany(fetch=FetchType.LAZY, mappedBy="user", cascade=CascadeType.PERSIST) 
private List<Address> addresses; 

The documentation says that by default, no operation is cascaded. It also states that the cascade operation is optional, so it really depends on the implementation you use.

, , , . .

+10

, , Cascading. , Address User. , CascadeType.

@OneToMany(fetch=FetchType.LAZY, mappedBy="user", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
private List<Address> addresses;

JPA, :

@OneToMany(fetch=FetchType.LAZY, mappedBy="user", cascade = CascadeType.ALL)
private List<Address> addresses;

, , Address User Address User.

!... CascadeType.REMOVE , User Address ( ).

+5

You are using the oneToMany annotation. In my opinion, you should keep the parent class (USER) if you want to add a child class (address) to it.

By storing the User class, you know that the JPA knows which row is being updated.

0
source

All Articles