I have the following structure:
@MappedSuperclass
public abstract class Action {
...
}
And than few entities extending Action, for example:
@Entity(name="chemistry")
public class Chemistry extends Action implements Serializable {
...
}
In order to execute the persistance I used JPARepository:
@NoRepositoryBean
public interface ActionRepository<T extends Action> extends JpaRepository<T, String> {
...
}
And an interface for the children:
@Component
@Service
@Repository
interface ChemistryRepository extends ActionRepository<Chemistry> {
}
The persistance mechanism works fine for all the entities inheriting from Action. Now I have a problem. I have another table in the DB, called 'action_params', including additional key-value parameters which related to a specific action (ie chemistry). I have the appropriate entity:
@Entity(name="action_params")
public class ActionParams implements Serializable {
...
}
In the DB I cannot use foreign key since the key is not related to a specific table but any table inherits from Action. this is ok, I would like to use a query in order to get for each Action the records from the table action_params, based on its id.
And I would like to add a List to Action, mapping those records as a list. I tried using hibernate but any query returns empty (The following code was added into Action class):
@OneToMany
@JoinFormula(value="select p from action_params p where p.action_id = id")
@Transient
protected List<ActionParams> actionParamsList;
This is not working. I also tried to autowire the ActionParamsRepository into Action, but of course there is no way doing it and I could not think of any other way.
Your advice will be much appreciated
What strong reason you have to use a JPA relation?
If there is no such reason, I'd suggest you to implement a new finder method in the ActionParams repository. Something like this:
@Query("select p from action_params p where p.action_id = :actionId")
List<ActionParams> findAllByActionId(@Param("actionId") Long actionId )
I suppose the type is Long. Replace it if you have other type.
You can use @Formula , so let me explain, when you put a query on the formula, in Hibernate is added a subquery.
@Formula("(SELECT p FROM action_params p WHERE p.action_id = id)")
private List<ActionParams> actionParamsList;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.