Spring JPA Repository dynamic query [duplicate]

Per JB Nizet and the spring-data documentation, you should use a custom interface + repository implementation. Create an interface with the method: public interface MyEntityRepositoryCustom { List<User> findByFilterText(Set<String> words); } Create an implementation: @Repository public class MyEntityRepositoryImpl implements MyEntityRepositoryCustom { @PersistenceContext private EntityManager entityManager; public List<User> findByFilterText(Set<String> words) { // implementation below } } Extend … Read more

Why to use @AllArgsConstructor and @NoArgsConstructor together over an Entity?

The JPA specification requires that all persistent classes (@Entity) have a no-arg constructor, public or protected. (note that this is not necessarily true when dealing with some implementation like Hibernate, see this answer). This is needed because JPA uses the default constructor method to create a bean class using the reflection API. Indeed if your … Read more

No qualifying bean of type [javax.persistence.EntityManager]

The EntityManager interface belongs to JPA and is implemented by JPA providers (such as Eclipse), not Spring, and it has its own injection annotation: @PersistenceContext. EntityManager objects are transaction-scoped and should not be exposed as beans as you’re doing. Instead, either use the JPA annotation to inject the EntityManager: @PersistenceContext EntityManager em; or, since it … Read more

JPA one-to-many filtering

Another hibernate way of doing it with @Where: @Entity public class System { @Id @Column(name = “ID”) private Integer id; @OneToMany(mappedBy = “system”) @Where(clause = “active = true”) private Set<Systemproperty> systempropertys; } @Entity public class Systemproperty { @Id @Column(name = “ID”) private Integer id; @Id @Column(name = “ACTIVE”) private Integer active; }

How to join unrelated entities with the JPA Criteria API

First: Foreign key relationship are not only for navigating. They mainly serve to ensure that no spurious values are introduced in the relationship. They also may help the database for query optimization. I would advise you to reconsider that. Anyway, for creating a query that uses several unrelated entities, you need to put them as … Read more

Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException

The error comes from JpaTransactionManager line 403: TransactionSynchronizationManager.bindResource(getDataSource(), conHolder); The error means that the transaction manager is trying to bind the datasource (not the entity manager) to the thread, but the datasource is already there and this is unexpected. Note that the transaction manager had not started yet to bind the Entity Manager to the … Read more

Mapping Set using @ElementCollection

for future googlers! finally i managed to solve the problem, i just had to put the annotations somewhere else in my code , @ElementCollection(targetClass = Days.class) @CollectionTable(name = “days”, joinColumns = @JoinColumn(name = “rule_id”)) @Column(name = “daysOfWeek”, nullable = false) @Enumerated(EnumType.STRING) public Set<Days> getDays() { return days; } as you can see i wrote the … Read more

How to implement a Spring Data repository for a @MappedSuperclass

It’s just a matter of annotating the abstract Repository as @NoRepositoryBean: @NoRepositoryBean public interface Dao<T extends BaseClass, E extends Serializable> extends CrudRepository<T, E> { Iterable<T> findByActive(Boolean active); } This way Spring relies on the underlying repository implementation to execute the findByActive method. Regarding to the annotation type restriction issue, it’s not possible to declare an … Read more

tech