0

I am using an Employee and Company class as an example. In a Java persistence environment (Hibernate), when a bean is processing an Employee class with its' own Entity Manager, how can I call a Company class bean method when that object has its' own Entity Manager? DO I have to do a one to one mapping or can I call the method via the Application Config somehow?

Thanks.

1 Answers1

0

You can use one to one mapping. I have implemented recently with product and team class. I have marked product as entity and team as entity. Below is the code which you would need to make it work. There are other ways to configure as well. In the below config you would require one table to store product, one table to store team and third team to store the productid and 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********

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.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;
    }

}
user3509208
  • 531
  • 1
  • 5
  • 15
  • Thanks for the reply. I was hoping I could just call the other object methods via the application context. This seems awfully heavy to just update a different object/table. – daggetlover Feb 26 '16 at 23:42