How to fix Hibernate error "object references an unsaved transient instance" when saving related entities?
Hello everyone! I'm a beginner Java developer and encountered a problem while working with Hibernate. I have two entities: `Author` and `Book`. One author can write many books, and each book references one author (@ManyToOne relationship). \n\nWhen I try to save a new book with a new author, Hibernate throws an exception: `org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.example.Book.author`.\n\nHere is my code:\n\n```java\n@Entity\npublic class Author {\n @Id\n @GeneratedValue(strategy = GenerationType.IDENTITY)\n private Long id;\n private String name;\n \n @OneToMany(mappedBy = "author", cascade = CascadeType.ALL)\n private List books = new ArrayList<>();\n}\n\n@Entity
No answers yet.
Log in to leave an answer