Hibernate获取链接到当前用户的对象

时间:2022-09-11 16:52:47

I'm currently a little blocked with this and I can't see it clearly.

我目前对此有点*,我看不清楚。

So I hope one of you have good idea's to help me.

所以我希望你们中的一个有好主意来帮助我。

The important code at the moment :

目前重要的代码:

@Entity
@Table(name = "T_NOTA_RECIPIENT")
public class NotaRecipient extends PersistentEntity {

    @Id
    @Column(name = "NOTA_RECIPIENT_SID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "STATUS", insertable = true, updatable = true)
    @Enumerated(EnumType.STRING)
    private Status status = Status.NEW;

    @ManyToOne
    @JoinColumn(name = "NOTA_SID", referencedColumnName = "NOTA_SID", nullable = false)
    private Nota nota;

    @ManyToOne
    @JoinColumn(name = "CREATOR_OFFICE_SID", referencedColumnName = "OFFICE_SID", nullable = false)
    private Office creator;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "notaRecipient")
    private Set<FollowUp> followUps;

    ...
}

Now, actually I don't want to load all the FollowUp who are in the DB but just the one of the current user.
But the problem is that I want to include the FollowUp so I can do database paging query.
We use hibernate, Spring Data and Query DSL with BooleanBuilder to "refine" our search.

现在,实际上我不想加载DB中的所有FollowUp,而只是加载当前用户之一。但问题是我想要包含FollowUp所以我可以做数据库分页查询。我们使用hibernate,Spring Data和Query DSL与BooleanBuilder来“优化”我们的搜索。

I was thinking of using @Formula but this need to be a constant String so I can't include current userId in that.

我正在考虑使用@Formula,但这需要是一个常量String,所以我不能包含当前的userId。

Second solution could be setting the FollowUp as @Transient and fetch it myself in the DB and set it in mine service.
Problem here is that I can't use it as filter then or ordering by it.

第二个解决方案可能是将FollowUp设置为@Transient并在数据库中自己获取并将其设置为我的服务。这里的问题是我不能将它用作过滤器或按顺序排序。

@Formula doesn't have so much documentation, so is it possible to make a @Transient user and use that in the @Formula?

@Formula没有那么多文档,所以可以创建一个@Transient用户并在@Formula中使用它吗?

I asked some colleagues but they couldn't help me.

我问过一些同事,但他们无法帮助我。

So then it's the time for asking here. I can get the current user in the API, so that's no problem.

那么现在是时候问这里了。我可以在API中获取当前用户,这样就没问题了。

Anybody have alternative solutions?

有人有替代解决方案吗?

1 个解决方案

#1


2  

You can define a mapping with expression

您可以使用表达式定义映射

@JoinColumnOrFormula(formula=@JoinFormula(value="(SELECT f.id 
                                              FROM follow_up_table f 
                                              WHERE f.nota_id=id
                                              and f.user_id={USER_ID})", 
 referencedColumnName="...") 

And then add hibernate interceptor (see the example) and change the SQL on fly replacing {USER_ID} with real value in the

然后添加hibernate拦截器(参见示例)并在运行中更改SQL替换{USER_ID}中的实际值

/**
 * Called when sql string is being prepared. 
 * @param sql sql to be prepared
 * @return original or modified sql
 */
public String onPrepareStatement(String sql);

#1


2  

You can define a mapping with expression

您可以使用表达式定义映射

@JoinColumnOrFormula(formula=@JoinFormula(value="(SELECT f.id 
                                              FROM follow_up_table f 
                                              WHERE f.nota_id=id
                                              and f.user_id={USER_ID})", 
 referencedColumnName="...") 

And then add hibernate interceptor (see the example) and change the SQL on fly replacing {USER_ID} with real value in the

然后添加hibernate拦截器(参见示例)并在运行中更改SQL替换{USER_ID}中的实际值

/**
 * Called when sql string is being prepared. 
 * @param sql sql to be prepared
 * @return original or modified sql
 */
public String onPrepareStatement(String sql);