2016-02-25 23 views
0

我以雇员和公司类为例。在Java持久化环境(Hibernate)中,当一个bean使用其自己的Entity Manager处理Employee类时,如果该对象具有其自己的Entity Manager,该如何调用公司类Bean方法?我必须做一对一的映射,还是可以通过应用程序配置以某种方式调用方法?java持久化交叉对象方法访问

谢谢。

回答

0

您可以使用一对一映射。我最近实施了产品和团队课程。我将产品标记为实体和团队作为实体。以下是您需要使其工作的代码。还有其他的配置方式。在下面的配置中,您需要一个表来存储产品,一个表存储团队,第三个团队存储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; 
    } 

} 
+0

感谢您的回复。我希望我可以通过应用程序上下文调用其他对象方法。这似乎非常沉重,只是更新不同的对象/表。 – daggetlover