Custom @Column JPA Annotations, how to?

左心房为你撑大大i 提交于 2021-02-08 03:47:32

问题


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;

or

//@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.

Thank you!


回答1:


Generate a new Class that implements UserType and use this annotation:

@Type(type="fully.qualified.name.of.YourUserType")

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.



来源:https://stackoverflow.com/questions/23346441/custom-column-jpa-annotations-how-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!