Организация данных через Hibernate через 3 таблицы

 
 
 
Сообщения:3
Доброго дня!

Имеется задача реализации работы с прайс листами через три таблицы SKU(long id_sku, String name_sku), PriceList(long id_pl, String namepl) и skuPrice(long id_sku, long id_pl, float price).

На текущий момент реализовано через две таблицы skuPrice(long id_pl, long id_skuPrice, String name_skuPrice, float price) и PriceList(long id_pl, String name_pl).

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import businesslogic.sku.StoreKeepingUnit;

public class PriceList {
	private long id;
	private String name;
	private Set<StoreKeepingUnitPrice> products = new HashSet<StoreKeepingUnitPrice>();
	
	public PriceList() {
		
	}
	
	public PriceList(long id, String name) {
		this.id = id;
		this.name = name;
	}
	
	public PriceList(long id, 
			String name,
			HashSet<StoreKeepingUnitPrice> map) {
		this.id = id;
		this.name = name;
		this.products = map;
	}

	public void addProduct(StoreKeepingUnitPrice product) {
		products.add(product);
	}
	
	public int getProductsPricesCount() {
		return products.size();
	}

	public boolean isProductPricesVoid() {
		if(getProductsPricesCount() == 0)
			return true;
		else 
			return false;
	}
	
	public void addSKUPrice(StoreKeepingUnitPrice skuPrice) {
		products.add(skuPrice);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	

	public Set<StoreKeepingUnitPrice> getProducts() {
		return products;
	}

	public void setProducts(Set<StoreKeepingUnitPrice> products) {
		this.products = products;
	}
	
	public HashSet<StoreKeepingUnit> getSKUs() {
		HashSet<StoreKeepingUnit> result = new HashSet<StoreKeepingUnit>();
		for (StoreKeepingUnitPrice skuPrice: this.getProducts()) {
			result.add(skuPrice.getSku());
		}
		return result;
	}
	
	public int getSkuCount() {
		return getSKUs().size();
	}
	
	public double getPriceBySKU(StoreKeepingUnit sku) {
		double result = -1.0;
		for(StoreKeepingUnitPrice skuPriceInProducts: this.getProducts()) {
			if(sku.equals(skuPriceInProducts.getSku()) == true) {
				return skuPriceInProducts.getPrice();
			}
		}
		return result;
	}
}


import businesslogic.sku.StoreKeepingUnit;

public class StoreKeepingUnitPrice {
	private StoreKeepingUnit sku;
	
	//For test only id and name
	private long id;
	private String name;
	
	private double price;
	
	//For ORM, but it should be delete
	private PriceList priceList; 
	
	public StoreKeepingUnitPrice() {
		
	}
	
	public StoreKeepingUnitPrice(StoreKeepingUnit sku, double price) {
		this.sku = sku;
		
		//For test only
		this.name = sku.getName();
		
		this.price = price;
	}
	
	public StoreKeepingUnitPrice(long SKUInnerID,
		String SKUInnerName) {
	this.sku = new StoreKeepingUnit(SKUInnerID, SKUInnerName);
	}
	
	public StoreKeepingUnitPrice(long SKUInnerID,
			String SKUInnerName, float price) {
		this.sku = new StoreKeepingUnit(SKUInnerID, SKUInnerName);
		this.price = price;
	}
	
	public boolean isPriceVoid() {
		if(getPrice() == 0)
			return true;
		else
			return false;
	}
	
	public StoreKeepingUnit getSku() {
		return sku;
	}
	
	public void setSku(StoreKeepingUnit sku) {
		this.sku = sku;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PriceList getPriceList() {
		return priceList;
	}

	public void setPriceList(PriceList priceList) {
		this.priceList = priceList;
	}

}


public class StoreKeepingUnit {
	private long id;
	private String name;
	
	public StoreKeepingUnit() {
		
	}
	
	public StoreKeepingUnit(long SKUInnerID, String SKUInnerName) {
		this.id = SKUInnerID;
		this.name = SKUInnerName;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
			
}


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="businesslogic.pricelist">
	<class name="PriceList" table="PL">
		<id name="id" column="pl_id">
			<generator class="increment" />
		</id>
		
		<property name="name" column="name" />
		
		 
		<set name="products" table="PL" inverse="true" lazy="true" fetch="select">
			<key>
                <column name="pl_id" not-null="true" />
            </key>
			<one-to-many class="businesslogic.pricelist.StoreKeepingUnitPrice" />
        </set>
		
	</class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="businesslogic.pricelist">
	<class name="StoreKeepingUnitPrice" table="sku">
		<id name="id" column="sku_id">
			<generator class="increment" />
		</id>
		
		<property name="name" type="string">
			<column name="name"></column>
		</property>
		
		 
		<many-to-one name="priceList" class="businesslogic.pricelist.PriceList" fetch="select">
            <column name="pl_id" not-null="true" />
        </many-to-one>
	 	
	</class>
</hibernate-mapping>
 
Модераторы:Нет
Сейчас эту тему просматривают:Нет