Frage

I'm trying to save 2 entities in AppFuse (Struts2, Hibernate and Spring) at once, Here is an example (Address and person are new objects):

person.setAddress(address);
personManager.save(person);

But this doesn't work, i get this exception:

object references an unsaved transient instance - save the transient
instance before merge

I have to do:

addressManager.save(address);
person.setAddress(address);
personManager.save(person);

In person model i have declared Address like that:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "person", cascade= CascadeType.ALL)
public Address getAddress() {
    return this.address
}

Are there any way to save this new entities at once?

Thanks in advance..!

War es hilfreich?

Lösung

Below may helpful for you

Have you fallowed as given in docs_oracle_javax_persistence_OneToMany.html

Example 1: One-to-Many association using generics

In Customer class:

@OneToMany(cascade=ALL, mappedBy="customer")
public Set getOrders() { return orders; }

In Order class:

@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() { return customer; }


Example 2: One-to-Many association without using generics

In Customer class:

@OneToMany(targetEntity=com.acme.Order.class, cascade=ALL,
        mappedBy="customer")
public Set getOrders() { return orders; }

In Order class:

@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() { return customer; }

You can do as given in this Example OneToManyTargetEntity.

Look at these threads:
stackoverflow_4011472
stackoverflow_9032998

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top