简体   繁体   English

我可以简化 Hibernate 注释吗?

[英]Can I simplify Hibernate annotations?

For some reasons, I'd like to use different value generated sequences rather than one for all tables, like:出于某些原因,我想对所有表使用不同的值生成序列,而不是一个,例如:

@Entity
public class User {
       @Id
       @TableGenerator(name = "user", table = "hibernate_sequences", pkColumnName = "sequence_name",
           valueColumnName = "next_val", pkColumnValue = "user", allocationSize = 1)
       @GeneratedValue(strategy = GenerationType.TABLE, generator = "user")
       private Integer id;
}

When my application has more tables(now is more than 50), I want to simplify this code like:当我的应用程序有更多表(现在超过 50 个)时,我想简化此代码,例如:

@Entity
public class User {
    @Id
    @MyTableGenerator(name = "user")
    private Integer id;
}

Is there some way to do this?有没有办法做到这一点? In this case, I want to accomplish two goals.在这种情况下,我想完成两个目标。 First is set some default values to annotation (hibernate annotation, not my custom annotation).首先是为注解设置一些默认值(休眠注解,而不是我的自定义注解)。 Second is use one of my custom annotation represent multiple hibernate annotation.其次是使用我的自定义注释之一代表多个 hibernate 注释。

I'm sorry if I have not explained my question well, and please point it out.如果我没有很好地解释我的问题,我很抱歉,请指出。

You can try something like this:你可以尝试这样的事情:

@Entity
@SequenceGenerator(name = "USER_ID_GENERATOR", sequenceName = "SEQ_USER")
public class User {

       @Id
       @GeneratedValue(generator = "USER_ID_GENERATOR", strategy = GenerationType.IDENTITY)
       private Integer id;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM