0
我以僱員和公司類爲例。在Java持久化環境(Hibernate)中,當一個bean使用其自己的Entity Manager處理Employee類時,如果該對象具有其自己的Entity Manager,該如何調用公司類Bean方法?我必須做一對一的映射,還是可以通過應用程序配置以某種方式調用方法?java持久化交叉對象方法訪問
謝謝。
我以僱員和公司類爲例。在Java持久化環境(Hibernate)中,當一個bean使用其自己的Entity Manager處理Employee類時,如果該對象具有其自己的Entity Manager,該如何調用公司類Bean方法?我必須做一對一的映射,還是可以通過應用程序配置以某種方式調用方法?java持久化交叉對象方法訪問
謝謝。
您可以使用一對一映射。我最近實施了產品和團隊課程。我將產品標記爲實體和團隊作爲實體。以下是您需要使其工作的代碼。還有其他的配置方式。在下面的配置中,您需要一個表來存儲產品,一個表存儲團隊,第三個團隊存儲productid和teamid。
***Product Class**
import java.io.Serializable;
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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@Entity
@Table(name="Product")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Product implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5392649457041674962L;
@Id
@Column(name="productId")
@GeneratedValue
private Long productId;
@Column(name="productName")
private String productName;
@Column(name="productHasVariations")
private String productHasVariations;
@Column(name="productImgPath")
private String productImgPath;
@Column(name="productDesc")
private String productDesc;
// //---------------------------------------item mapped to league------------------------------------------//
@JsonBackReference
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinTable(
name="ProductLeague",
joinColumns= @JoinColumn(name="productId"),
inverseJoinColumns = @JoinColumn(name="leagueId")
)
private League league;
// //--------------------------------------------------------------------------------------------------------//
//
// //---------------------------------------item mapped to category------------------------------------------//
@JsonBackReference
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(
name="ProductCategory",
joinColumns= @JoinColumn(name="productId"),
inverseJoinColumns = @JoinColumn(name="categoryId")
)
private Category category;
// //--------------------------------------------------------------------------------------------------------//
//
// //---------------------------------------item mapped to team------------------------------------------//
@JsonBackReference
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(
name="ProductTeam",
joinColumns= @JoinColumn(name="productId"),
inverseJoinColumns = @JoinColumn(name="teamId")
)
private Team team;
// //--------------------------------------------------------------------------------------------------------//
//
// //---------------------------------------item mapped to flags such as featured, sale, hot, new------------//
@JsonBackReference
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinTable(
name="ProductFlag",
joinColumns= @JoinColumn(name="productId"),
inverseJoinColumns = @JoinColumn(name="flagId")
)
private Flag flag;
//--------------------------------------------------------------------------------------------------------//
//
//---------------------------------------item mapped to sizes ------------//
@JsonBackReference
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@OrderBy("sizeId asc")
@JoinTable(
name="ProductSize",
joinColumns= @JoinColumn(name="productId"),
inverseJoinColumns = @JoinColumn(name="sizeId")
)
private Set<Size> size;
// //--------------------------------------------------------------------------------------------------------//
//---------------------------------------item mapped to prices ------------//
@JsonBackReference
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@OrderBy("priceId asc")
@JoinTable(
name="ProductPrice",
joinColumns = { @JoinColumn(name="productId")
},
inverseJoinColumns = @JoinColumn(name="priceId")
)
private Set<Price> price;
// //--------------------------------------------------------------------------------------------------------//
// //--------------------------------------------------------------------------------------------------------//
//---------------------------------------item mapped to discounts ------------//
@JsonBackReference
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinTable(
name="ProductDiscount",
joinColumns = { @JoinColumn(name="productId")
},
inverseJoinColumns = @JoinColumn(name="discountId")
)
private Discount discount;
// //--------------------------------------------------------------------------------------------------------//
@Transient
private Long productQuantity;
@Transient
private String productPriceBeforeDiscount;
@Transient
private String productPriceAfterDiscount;
@Transient
private String productSelectedSize;
public Flag getFlag() {
return flag;
}
public void setFlag(Flag flag) {
this.flag = flag;
}
public Long getProductId() {
return this.productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return this.productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public void setProductHasVariations(String productHasVariations) {
this.productHasVariations = productHasVariations;
}
public String getProductHasVariations() {
return productHasVariations;
}
public void setLeague(League league) {
this.league = league;
}
public League getLeague() {
return league;
}
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
public Team getTeam() {
return this.team;
}
public void setTeam(Team team) {
this.team = team;
}
public String getProductImgPath() {
return productImgPath;
}
public void setProductImgPath(String productImgPath) {
this.productImgPath = productImgPath;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductDesc() {
return productDesc;
}
public void setSize(Set<Size> size) {
this.size = size;
}
public Set<Size> getSize() {
return size;
}
public void setPrice(Set<Price> price) {
this.price = price;
}
public Set<Price> getPrice() {
return price;
}
public void setProductQuantity(Long productQuantity) {
this.productQuantity = productQuantity;
}
public Long getProductQuantity() {
return productQuantity;
}
public void setDiscount(Discount discount) {
this.discount = discount;
}
public Discount getDiscount() {
return discount;
}
public void setProductPriceAfterDiscount(String productPriceAfterDiscount) {
this.productPriceAfterDiscount = productPriceAfterDiscount;
}
public String getProductPriceAfterDiscount() {
return productPriceAfterDiscount;
}
public void setProductPriceBeforeDiscount(String productPriceBeforeDiscount) {
this.productPriceBeforeDiscount = productPriceBeforeDiscount;
}
public String getProductPriceBeforeDiscount() {
return productPriceBeforeDiscount;
}
public void setProductSelectedSize(String productSelectedSize) {
this.productSelectedSize = productSelectedSize;
}
public String getProductSelectedSize() {
return productSelectedSize;
}
}
**** TeamClass ********
進口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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name="Team")
public class Team implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5969057417203282157L;
@Id
@Column(name="teamId")
@GeneratedValue
private Integer teamId;
@Column(name="teamName")
private String teamName;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinTable(
name="ProductTeam",
joinColumns = @JoinColumn(name="teamId"),
inverseJoinColumns = @JoinColumn(name="productId")
)
public Set<Product> product;
public Integer getTeamId() {
return this.teamId;
}
public void setTeamId(Integer teamId) {
this.teamId = teamId;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Set<Product> getProduct() {
return product;
}
public void setProduct(Set<Product> product) {
this.product = product;
}
}
感謝您的回覆。我希望我可以通過應用程序上下文調用其他對象方法。這似乎非常沉重,只是更新不同的對象/表。 – daggetlover