How to fix the Hibernate “object references an unsaved transient instance – save the transient instance before flushing” error

You should include cascade=”all” (if using xml) or cascade=CascadeType.ALL (if using annotations) on your collection mapping. 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 … Read more

How can I make a JPA OneToOne relation lazy

First off, some clarifications to KLE‘s answer: Unconstrained (nullable) one-to-one association is the only one that can not be proxied without bytecode instrumentation. The reason for this is that owner entity MUST know whether association property should contain a proxy object or NULL and it can’t determine that by looking at its base table’s columns … Read more

Hibernate show real SQL [duplicate]

Can I see (…) the real SQL If you want to see the SQL sent directly to the database (that is formatted similar to your example), you’ll have to use some kind of jdbc driver proxy like P6Spy (or log4jdbc). Alternatively you can enable logging of the following categories (using a log4j.properties file here): log4j.logger.org.hibernate.SQL=DEBUG … Read more

JPA JoinColumn vs mappedBy

The annotation @JoinColumn indicates that this entity is the owner of the relationship (that is: the corresponding table has a column with a foreign key to the referenced table), whereas the attribute mappedBy indicates that the entity in this side is the inverse of the relationship, and the owner resides in the “other” entity. This … Read more

What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

From the community documentation: hibernate.hbm2ddl.auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. e.g. validate | update | create | create-drop So the list of possible options are, validate: validate the schema, makes no changes … Read more

Hibernate Unknown entity (I know it has been asked before…) [duplicate]

It is already a classical problem: you can’t use Hibernate 4 configuration code to configure Hibernate 5. https://stackoverflow.com/a/32711654/3405171 Just use: Configuration configuration = new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); Above code works, if you have properties in hibernate.properties. You have it in hibernate.cfg.xml. I can’t remember, probably, it will work for this situation too. Any way, … Read more