1. 程式人生 > >lombok 和 hibernate 配合對實體類註解

lombok 和 hibernate 配合對實體類註解

lombok的@Data註解,可以在編譯過程中自動插入get/set ,有參構造,hash,toString 等方法

但是這個類如果是表中的對映類,使用的hibernate的註解對映,怎麼辦,請看hibernate對映註解的例項,

問題:

可以看到hibernate註解實體類時,會在每個get方法上加 @Column註解,主鍵上還會加@GenericGenerator,@GeneratedValue,@Id註解,按照lombok 去掉get/set的邏輯這就很難辦啊

package com.money.test.entry;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;

/**
 * MoneyType entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "money_type", catalog = "httpclientcrawler")

public class MoneyType implements java.io.Serializable {

	// Fields

	private String TId;
	private String typeName;
	private Set<MoneyTypeLittle> moneyTypeLittles = new HashSet<MoneyTypeLittle>(0);//外來鍵

	// Constructors

	/** default constructor */
	public MoneyType() {
	}

	/** full constructor */
	public MoneyType(String typeName, Set<MoneyTypeLittle> moneyTypeLittles) {
		this.typeName = typeName;
		this.moneyTypeLittles = moneyTypeLittles;
	}

	// Property accessors
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@Id
	@GeneratedValue(generator = "generator")

	@Column(name = "t_id", unique = true, nullable = false, length = 64)

	public String getTId() {
		return this.TId;
	}

	public void setTId(String TId) {
		this.TId = TId;
	}

	@Column(name = "type_name", length = 64)

	public String getTypeName() {
		return this.typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "moneyType")

	public Set<MoneyTypeLittle> getMoneyTypeLittles() {
		return this.moneyTypeLittles;
	}

	public void setMoneyTypeLittles(Set<MoneyTypeLittle> moneyTypeLittles) {
		this.moneyTypeLittles = moneyTypeLittles;
	}

}

lombok 的@Getter

後來在網上搜索相關的內容,沒什麼發現,但是看到一個介紹lombok註解的文章,提到了@Getter註解有一個屬性,可以放入其他註解,

但是jdk7 和 jak 8 的寫法不同

/**
	 * Any annotations listed here are put on the generated method.
	 * The syntax for this feature depends on JDK version (nothing we can do about that; it's to work around javac bugs).<br>
	 * up to JDK7:<br>
	 *  {@code @Getter(
[email protected]
__({@AnnotationsGoHere}))}<br> * from JDK8:<br> * {@code @Getter(onMethod_={@AnnotationsGohere})} // note the underscore after {@code onMethod}. * * @return List of annotations to apply to the generated getter method. */ AnyAnnotation[] onMethod() default {};

lombok 配合 hibernate 註解實體類例項

如圖所示,就是在每個屬性上面標註@Getter註解 的onMethod上面將@Column等註解加入進來,感覺這樣配起來也很麻煩,也許lombok 適合那種資料實體類,不適合這種和表關聯的實體類

package com.money.test.entry;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * MoneyUse entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "money_use", catalog = "httpclientcrawler")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MoneyUse implements java.io.Serializable {

	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Getter(onMethod_={@GenericGenerator(name = "generator", strategy = "uuid.hex"),@Id,@GeneratedValue(generator = "generator"),
			@Column(name = "m_id", unique = true, nullable = false, length = 64)})
	private String mId;
	
	@Getter(onMethod_={@Column(name = "tl_id", length = 64)})
	private String tlId;
	
	@Getter(onMethod_={@Column(name = "order_amount", precision = 25, scale = 8)})
	private BigDecimal orderAmount;
	
	@Getter(onMethod_={@Column(name = "order_time")})
	private Date orderTime;
	
	@Getter(onMethod_={@Column(name = "is_out")})
	private Boolean isOut;//mysql 資料庫 tinyint 欄位表示Boolean ; 0 代表false ,1 代表true
	
	@Getter(onMethod_={@Column(name = "is_must")})
	private Boolean isMust;
	
	@Getter(onMethod_={@Column(name = "standard_amount", precision = 25, scale = 8)})
	private BigDecimal standardAmount;//標準花費
}