[英]How to combine annotations for hibernate mapping?
正如尼爾斯托克頓所提到的,元注釋可能會成為下一個 JPA 2.2 版的一部分。
現在,使用 JPA 2.1,我可以將@Embeddable類用於 id( @EmbeddedId )和非 id 字段( @Embedded )
請注意,對於@Embeddable,我可以使用泛型類,因此它對任何類型都有用+我可以輕松覆蓋我的列屬性:
@Embeddable
@Getter @Setter @NoArgsConstructor // Lombok library
public class EmbeddableGeneric<T> {
@Column
// other annotations
T myField;
...
}
在我的實體類中:
@Entity
@Getter @Setter @NoArgsConstructor // You know now what's this!
public class Person {
@Id
@GeneratedValue
private Long id;
@Embedded
@AttributeOverride(name = "myField", column = @Column(name = "STRING_FIELD"))
private EmbeddableGeneric<String> myString;
...
}
讓我們等待 JPA 2.2 來克服這種冗長的問題。
理論上應該是這樣的:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.persistence.Id;
import javax.persistence.TableGenerator;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CustomIdAnnotation {
TableGenerator generator() default @TableGenerator(name = "EVENT_GEN",
table = "SEQUENCES",
pkColumnName = "SEQ_NAME",
valueColumnName = "SEQ_NUMBER",
pkColumnValue = "ID_SEQUENCE",
allocationSize = 1);
Id id();
}
但是,我認為這不會起作用,因為持久性提供程序(Hibernate、EclipseLink 等)直接在您的實體類中處理包javax.persistence.*
+ 提供程序特定的注釋的注釋。 因此,如果您打算編寫自己的持久性提供程序,這可能會奏效。 ( 實現 JSR-000338 JPA 2.1 規范)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.