1

I have an object called invoice that has a field which is a complex object

public class Invoice {
    @ManyToOne
    @JoinColumn(name = "id_site_to")
    private Site siteTo;
}

public class Site {
    @Id
    @GeneratedValue
    @Column(name = "id_site")
    private long id;

    private String description;
    ...
}

in a point of my code, I create an Invoice from a DTO. The so-created invoice contains a Site without all fields populated but only the ID. When I try to save this invoice instance (with JPA repository), I get

org.hibernate.TransientPropertyValueException: Not-null property references a transient value

even if the site Id exists in the database. There's a way to perform this save without having to first get the site from the database with a select?

dylaniato
  • 516
  • 9
  • 23

2 Answers2

2

If you use EntityManager you can try to call getReference(), refer What is the difference between EntityManager.find() and EntityManger.getReference()? for details.

With Hibernate's Session you can use load() as well.

All this methods return proxies without access to the database.

One note

With Hibernate's Session I use your way of setting an association by object with the id only. Everything worked fine.

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
0

Change the creation order, this error means that you are creating a reference to an object that it's not created still. Create the Site and then the Invoice.

  • 2
    He already has the `Site` in the database. He wants to set a fake `Site` with filling `id` only, without loading the whole `Site` from the database. – v.ladynev Jan 29 '16 at 11:52