When and why JPA entities should implement the Serializable interface?

The question is in the title. Below I just described some of my thoughts and findings. When I had a very simple domain model (3 tables without any relations), all my entities did NOT implement the Serializable interface. But when the domain model became more complex, I got a RuntimeException, saying that one of my … Read more

Spring boot – Not a managed type

I use Spring boot+JPA and having a problem while starting the service. Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.nervytech.dialer.domain.PhoneSettings at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219) at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68) at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65) at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:145) at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:89) at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:69) at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:177) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225) at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562) Here is the Application.java file, @Configuration @ComponentScan @EnableAutoConfiguration(exclude = { … Read more

How to persist a property of type List in JPA?

What is the smartest way to get an entity with a field of type List persisted? Command.java package persistlistofstring; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.Basic; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Persistence; @Entity public class Command implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id; @Basic List<String> arguments … Read more

@UniqueConstraint annotation in Java

I have a Java bean. Now, I want to be sure that the field should be unique. I am using the following code: @UniqueConstraint(columnNames={“username”}) public String username; But I’m getting some error: @UniqueConstraint is dissallowed for this location What’s the proper way to use unique constraints? Note: I am using play framework. 12 Answers 12

Can someone explain mappedBy in JPA and Hibernate?

I am new to hibernate and need to use one-to-many and many-to-one relations. It is a bi-directional relationship in my objects, so that I can traverse from either direction. mappedBy is the recommended way to go about it, however, I couldn’t understand it. Can someone explain: what is the recommended way to use it? what … Read more

How does JPA orphanRemoval=true differ from the ON DELETE CASCADE DML clause

I am a little confused about the JPA 2.0 orphanRemoval attribute. I think I can see it is needed when I use my JPA provider’s DB generation tools to create the underlying database DDL to have an ON DELETE CASCADE on the particular relation. However, if the DB exists and it already has an ON … Read more

Map enum in JPA with fixed values?

I’m looking for the different ways to map an enum using JPA. I especially want to set the integer value of each enum entry and to save only the integer value. @Entity @Table(name = “AUTHORITY_”) public class Authority implements Serializable { public enum Right { READ(100), WRITE(200), EDITOR (300); private int value; Right(int value) { … Read more

Make hibernate ignore class variables that are not mapped [duplicate]

This question already has answers here: What is the easiest way to ignore a JPA field during persistence? (10 answers) Closed 5 years ago. I thought hibernate takes into consideration only class variables that are annotated with @Column. But strangely today when I added a variable (that is not mapped to any column, just a … Read more