自定义@Column JPA批注,如何?
问题描述:
在声明实体POJO时,一直努力寻找创建一些自定义JPA注释以替换重复字段的解决方案.有什么帮助吗?这是我想要达到的目标:
Been trying hard to search for a solution to create some Custom JPA Annotations to replace repetitive fields when declaring Entity POJOs. Any help? Here what I am trying to achieve:
//@Column(name = "is_enabled", nullable = false, columnDefinition = "tinyint(1) DEFAULT 1")
@ColumnBooleanNotNullDefaultOne
private Boolean isEnabled;
或
//@Column(name = "created", nullable = false, updatable = false, insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@ColumnTimestamp
private Timestamp created;
但是,我的尝试失败了……
However, my attempts are failing...
@Target({METHOD, FIELD})
@Retention(RUNTIME)
@Column // <-- Error here (The annotation @Column is disallowed for this location.)
public @interface BooleanNotNullDefaultOne
{
}
任何帮助都必须得到重视.
Any help is definatelly aprecciated.
谢谢!
答
生成一个实现UserType的新类,并使用以下注释:
Generate a new Class that implements UserType and use this annotation:
@Type(type="fully.qualified.name.of.YourUserType")
此外,@ Column批注只能在方法或变量上使用.查看Column接口定义的@Target,以了解原因.
Also, the @Column annotation can only be used on a method or variable. Take a look at the @Target of the Column interface definition to understand why.