0

I have read many topics about mapping with JPA + Hibernate, but after trying several things I can not get the expected result.

I have declared all my unidirectional relationships since I do not see the need to bidirect them

My objects to map are: Client has a Country and a list of addresses.

@Table(name = "Client")
@Entity
public class Client
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    private Long id;

    @OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST })
    @JoinColumn(name="country_id", unique = false, /*nullable = false,*/ insertable = true, updatable = false, foreignKey = @ForeignKey(name = "country_fk0"))
    private Country country;

    @OneToMany(cascade = { CascadeType.PERSIST }, orphanRemoval = true)
    @JoinColumn(name = "address_id",/* nullable = false,*/ foreignKey = @ForeignKey(name = "address_fk0"))
    private List<Address> address;

//GETTERS / SETTERS
}

then I will evaluate the client and save it in a new ClientProcessed table, which will reference the Addresses and Country objects stored in my Client Object.

@Table(name = "ProcessedClient")
@Entity
public class ProcessedClient
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    private Long id;

    @OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST })
    @JoinColumn(name="client_evaluation_id", unique = false, /*nullable = false,*/ insertable = true, updatable = false, foreignKey = @ForeignKey(name = "evaluation_fk0"))
    private ClientEvaluation evaluation;

    @OneToOne(cascade = { CascadeType.MERGE })
    @JoinColumn(name="country_id", unique = false, /*nullable = false,*/, foreignKey = @ForeignKey(name = "country_fk1"))
    private Country country;

    @OneToMany(cascade = { CascadeType.MERGE}, orphanRemoval = true)
    @JoinColumn(name = "address_id",/* nullable = false,*/ foreignKey = @ForeignKey(name = "address_fk2"))
    private List<Address> addresses;

//GETTERS / SETTERS
}

So then when i do that:

Country country = new Country();
country.setId(1l); // (DB ID)

// I do the same with addresses

ProcessedClient processedClient = new ProcessedClient();
processedClient.setAddresses(addresses);
processedClient.setCountry(country);

this.getDao().save(processedClient);

Result:

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing

Thx

Matheus
  • 57
  • 1
  • 11
  • Possible duplicate of [object references an unsaved transient instance - save the transient instance before flushing](https://stackoverflow.com/questions/2302802/object-references-an-unsaved-transient-instance-save-the-transient-instance-be) – Echoinacup Feb 11 '19 at 16:50
  • But in that case my object are represented in the database, This happens because you have a collection in your entity, and that collection has one or more items which are not present in the database. By specifying the above options you tell hibernate to save them to the database when saving their parent. – Matheus Feb 11 '19 at 16:53

1 Answers1

0

From documentation:

Hibernate defines and supports the following object states:

  • Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session . ...
  • Persistent - a persistent instance has a representation in the database and an identifier value.

You should load country from database by id and then set it to ProcessedClient.

Tijkijiki
  • 792
  • 7
  • 19