@Transient
, @Basic
, @Column
, @NotNull
@Transient
: 선언된 필드는 영속화에서 제외@Basic
@Column
@NotNull
@Access(PROPERTY/FIELD)
@Access
: 프로퍼티 접근 전략
@Access(AccessType.PROPERTY)
: 게터/세터를 통한 접근
@Access(AccessType.FIELD)
: 필드를 통한 접근@Formula
@Formula
@Formula("CONCAT(SUBSTR(DESCRIPTION, 1, 12), '...')")
private string shortDescription;
@Formula("(SELECT AVG(B.AMOUNT) FROM BID B NHERE B.ITEN_ID = ID)")
private BigDecimal averageBidAmount;
@ColumnTransformer
@ColumnTransformer
@Column(name = "IMPERIALWEIGHT")
@ColumnTransformer(
read = "IMPERIALNEIGHT / 2.20462",
write = "? * 2.20462"
)
private double metricWeight;
List Item> result =
em.createquery("SELECT i FROM Item i WHERE i.metricNeight = :W")
.setParameter("w", 2.0)
.getResultList();
----------------------------------------------
// ...
where
i.IMPERIALNEIGHT / 2.20462 = ?
@Generated
@Generated
: 생성되는 프로퍼티를 표시할 때 사용
@CreationTimestamp
private LocalDate Createdon;
@UpdateTimestamp
private LocalDateTime LastModified;
@Column(insertable = false)
@ColumnDefault("1.00")
@Generated(org.hibernate.annotations.GenerationTime.INSERT)
private BigDecimal initialprice;
Hibernate 6.0.0부터 선택적으로 데이터베이스를 날짜 소스로 지정 가능한것으로 알려졌으나 적용되지 않은듯함..
@CreationTimestamp(source = SourceType.DB)
private Instant createdOn;
@UpdateTimestamp(source = SourceType.DB)
private Instant lastUpdatedOn;
@Temporal
어노테이션@Enumerated(EnumType.String)
@Embeddable
@EmbeddabLe
public class Address { // equals, hashCode 재정의 필요
@Notnull
@Column(nullable = false, Length = 5)
@Column(nullable = false)
private String street;
@NotNull
private String zipcode;
@NotNull
@Column(nullable = false)
private String city;
public Address() {
}
public Address (String street, String zipcode, String city) {
this.street = street;
this.zipcode = zipcode;
this.city = city;
}
// 게터
// equals, hashCode 재정의
}
@Entity
@Table(name = "USERS")
public class User {
@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
private Long id;
private Address homeAddress; // Address는 @Embeddable이므로, 애너테이션 지정 생략가능.
//...
}
@Embedded
, @AttributeOverride
@Entity
@Table(name = "USERS")
public class User {
//...
@Embedded // 생략가능
@AttributeOverride(name = "street", column = @column(name = "BILLING_STREET"))
@AttributeOverride(name = "zipcode", column = @Column(name = "BILLING_2IPCODE", Length = 5))
@AttributeOverride(name = "city", column = @Column(name = "BILLING_CITY"))
private Address bilLingAddress;
public Address getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(Address billingAddress) {
this.billingAddress = bilLingAddress;
}
//...
}
@Embeddable
public class Address {
@NotNull
@Column(nullable = false)
private String street;
@NotNull
@AttributeOverride(name = "name", column = @Column(name = "CITY", nullable = false))
private City city;
// ....
}
@Embeddable
public class City {
@NotNull
@Column(nullable = false, length = 5)
private String zipcode;
@NotNull
@Column(nullable = false)
private String name;
@NotNull
@Column(nullable = false)
private String country;
// ...
}
@AttributeOverride(name ="city.name")
@Type
, @Lob
@Entity
public class Item {
@Lob
private byte[] image;
@Lob
private String description;
}
@Entity
public class Item {
@Lob
private Blob image;
@Lob
private Clob description;
}
@Type
QEntity
public class Item {
@org.hibernate.anotations.Type(type = "yes_no")
private boolean verified = false;
}
@Convert
@NotNuLL
@Convert(converter = MonetaryAmountConverter.cLass)
@Column(name = "PRICE", Length = 63)
private MonetaryAmount buyNowprice;
@Converter
public class MonetaryAmountConverter
implements AttributeConvertercMonetaryAmount, String> {
@Override
public String convertToDatabaseColumn(MonetaryAmount monetaryAmount) {
return monetaryAmount.tostring();
}
@Override
public MonetaryAmount convertToEntityAttribute(String s) {
return MonetaryAmount.fromstring(s);
}
}
public abstract class Zipcode {
private string value;
public zipcode(String value) {
this.value = value;
}
public string getValue() {
return value;
}
@Override
public boolean equals(object o){
if (this == o) return true;
if (0 ==null il getclass() != 0.getclass()) return false;
Zipcode zipcode = (Zipcode) 0;
return objects.equals(value, zipcode.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
public class Germanzipcode extends Zipcode {
public Germanzipcode(String value) {
super(value);
}
}
@Converter
public class Zipcodeconverter implements AttributeConverterczipcode, String> {
@Override
public string convertToDatabaseColumn(Zipcode attribute) {
return attribute.getValue();
}
@Override
public Zipcode convertToEntityAttribute(String s) {
if (S.length() == 5) {
return new GermanZipcode(s);
}
if (S.length() == 4) {
return new SwissZipcode(s);
}
throw new IllegalArgumentException("Unsupported zipcode in database: " + s);
}
}
@Entity
@Table(name ="USERS")
public class user {
// ...
@Convert(converter = ZipcodeConverter.class, attributename = "city.zipcode")
private Address homeAddress;
}
@org.hibernate.annotations.TypeDefs({
@org.hibernate.annotations.TypeDef(
name = "monetary_amount_usd",
typeClass = MonetaryAmountuserType.class,
parameters = {@parameter(name = "convertTo", value = "USD")}
),
@org.hibernate.annotations.TypeDef(
name = "monetary_amount_eur",
typeclass = MonetaryAmountuserType.class,
parameters = {@parameter(name = "convertTo", value = "EUR")}
)
})
package com.manning.javapersistence.ch06.converter;
import org.hibernate.annotations.Parameter;
@Entity
public class Item {
@NotNull
@org.hibernate.annotations.Type(
type = "monetary_amount_usd"
)
@org.hibernate.annotations.Columns(columns ={
@Column(name = "BUYNONPRICE_AMOUNT"),
@Column(name = "BUYNOMPRICE_CURRENCY", Length = 3)
})
private MonetaryAmount buyNowprice;
@NotNull
@org.hibernate.annotations.Type(
type = "monetary_amount_eur"
)
@org.hibernate.annotations.Columns(columns ={
@Column(name = "INITIALPRICE_AMOUNT"),
@Column(name = "INITIALPRICE_CURRENCY", Length = 3)
})
private MonetaryAmount initialprice;
//...
}