码迷,mamicode.com
首页 > 其他好文 > 详细

BroadLeaf 下定义ORM对象:Province,City,Area

时间:2014-09-23 12:42:24      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:blog   io   java   ar   for   div   sp   on   c   

要点:BroadLeaf框架下定义ORM对象需要先定义一个接口, 然后在persistence-unit 的配置中进行声明,如不声明会报错:no persistent classes found for query class

IProvince:

import java.io.Serializable;

import javax.annotation.Nonnull;

public interface IProvince extends Serializable{
    
    @Nonnull
    public int getProv_Code();

    @Nonnull
    public String getProv_Name();

}

Province:

import javax.persistence.CascadeType;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "PROVINCE")
@SuppressWarnings("serial")
public class Province implements IProvince,Serializable{
	@EmbeddedId
	private ProvincePK prov_PK;

	
	@OneToMany(mappedBy = "province", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
	@Fetch(FetchMode.SUBSELECT)
	private Set<City> cities;	

	public int getProv_Code() {
		return prov_PK.getProv_Code();
	}

	public String getProv_Name() {
		return prov_PK.getProv_Name();
	}

	public Set<City> getCities() {
		return cities;
	}

	public void setCities(Set<City> cities) {
		this.cities = cities;
	}

	public ProvincePK getProv_PK() {
		return prov_PK;
	}

	public void setProv_PK(ProvincePK prov_PK) {
		this.prov_PK = prov_PK;
	}
	
}

ProvincePK(联合主键类):

import java.io.Serializable;

import javax.persistence.Embeddable;

@Embeddable
@SuppressWarnings("serial")
public class ProvincePK implements Serializable{
	
	private int prov_Code;
	
	private String prov_Name;

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof ProvincePK){
			if(((ProvincePK) obj).getProv_Code() == this.getProv_Code()
					&& ((ProvincePK) obj).getProv_Name().equals(this.getProv_Name())){
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return prov_Name.hashCode() + prov_Code;
	}

	public int getProv_Code() {
		return prov_Code;
	}

	public void setProv_Code(int prov_Code) {
		this.prov_Code = prov_Code;
	}

	public String getProv_Name() {
		return prov_Name;
	}

	public void setProv_Name(String prov_Name) {
		this.prov_Name = prov_Name;
	}
}

 

ICity:

public interface ICity extends Serializable{
    
    @Nonnull
    public int getCity_Code();

    @Nonnull
    public String getCity_Name();

}

City:

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "CITY")
@SuppressWarnings("serial")
public class City implements ICity,Serializable{
	@EmbeddedId
	private CityPK city_PK;
	
	@OneToMany(mappedBy = "city", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
	@Fetch(FetchMode.SUBSELECT)
	private Set<Area> areas;
	
	@ManyToOne
	@JoinColumns({@JoinColumn(name = "prov_Code"),@JoinColumn(name = "prov_Name")})
	private Province province;	

	public CityPK getCity_PK() {
		return city_PK;
	}

	public void setCity_PK(CityPK city_PK) {
		this.city_PK = city_PK;
	}

	public Set<Area> getAreas() {
		return areas;
	}

	public void setAreas(Set<Area> areas) {
		this.areas = areas;
	}

	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province;
	}

	@Override
	public int getCity_Code() {
		return city_PK.getCity_Code();
	}

	@Override
	public String getCity_Name() {
		return city_PK.getCity_Name();
	}
	
}

 

CityPK:

@Embeddable
@SuppressWarnings("serial")
public class CityPK implements Serializable{

	private int city_Code;
	
	private String city_Name;
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof ProvincePK){
			if(((CityPK) obj).getCity_Code() == this.getCity_Code()
					&& ((CityPK) obj).getCity_Name().equals(this.getCity_Name())){
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return city_Name.hashCode() + city_Code;
	}
	
	public int getCity_Code() {
		return city_Code;
	}

	public void setCity_Code(int city_Code) {
		this.city_Code = city_Code;
	}

	public String getCity_Name() {
		return city_Name;
	}

	public void setCity_Name(String city_Name) {
		this.city_Name = city_Name;
	}
	
}

 

IArea:

public interface IArea extends Serializable{
    
    @Nonnull
    public int getArea_Code();

    @Nonnull
    public String getArea_Name();

}

 

Area:

@Entity
@Table(name = "CITY_AREA")
@SuppressWarnings("serial")
public class Area implements IArea,Serializable{
	@EmbeddedId
	private AreaPK area_PK;
	
	@ManyToOne
	@JoinColumns({@JoinColumn(name = "city_Code"),@JoinColumn(name = "city_Name")})
	private City city;	

	public AreaPK getArea_PK() {
		return area_PK;
	}

	public void setArea_PK(AreaPK area_PK) {
		this.area_PK = area_PK;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	@Override
	public int getArea_Code() {
		return area_PK.getArea_Code();
	}

	@Override
	public String getArea_Name() {
		return area_PK.getArea_Name();
	}	
}

 

AreaPK:

@Embeddable
@SuppressWarnings("serial")
public class AreaPK implements Serializable{

	private int area_Code;
	
	private String area_Name;
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof ProvincePK){
			if(((AreaPK) obj).getArea_Code() == this.getArea_Code()
					&& ((AreaPK) obj).getArea_Name().equals(this.getArea_Name())){
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return area_Name.hashCode() + area_Code;
	}

	public int getArea_Code() {
		return area_Code;
	}

	public void setArea_Code(int area_Code) {
		this.area_Code = area_Code;
	}

	public String getArea_Name() {
		return area_Name;
	}

	public void setArea_Name(String area_Name) {
		this.area_Name = area_Name;
	}

}

 

BroadLeaf 下定义ORM对象:Province,City,Area

标签:blog   io   java   ar   for   div   sp   on   c   

原文地址:http://www.cnblogs.com/youneverdie/p/3987667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!