自定义@Column JPA批注,如何? [英] Custom @Column JPA Annotations, how to?
本文介绍了自定义@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.
这篇关于自定义@Column JPA批注,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文