hibernate 自连接 注解 item parentItem ( 非级联删除 , 级联查询)

简介:   @JsonAutoDetect 解决 该bug: Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource identified by t...

 

@JsonAutoDetect

解决 该bug: Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers () 

 

@org.hibernate.annotations.Proxy(lazy = false)

解决该bug: hibernate load() get() bug  ,,session 提前 close 问题

 


 

package com.cmcc.cailing.entity;

import java.io.Serializable;
import java.util.ArrayList; 
import java.util.List; 

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.slave4j.orm.hibernate.BaseEntity;
 
@JsonAutoDetect
@Entity
@Table(name = "t_company")
@org.hibernate.annotations.Proxy(lazy = false)
public class Company extends BaseEntity implements Serializable {

	
	/* `id` int(11) NOT NULL AUTO_INCREMENT,
	  `compName` varchar(50) DEFAULT NULL,
	  `compPhone` varchar(20) DEFAULT NULL,
	  `compEmail` varchar(20) DEFAULT NULL,
	  `compType` int(11) DEFAULT NULL,
	  `bossCompId` varchar(255) DEFAULT NULL,
	  `parentId` int(11) NOT NULL,
	  `orgCode` varchar(100) DEFAULT NULL,
	  `openType` int(11) DEFAULT NULL,*/
	 
	private static final long serialVersionUID = -5589729937828659285L; 
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id")
	private int id;

	@Column(name = "compname")
	private String compName;
	
	@Column(name = "compphone")
	private String compPhone;
	
	@Column(name = "compemail")
	private String compEmail;
	
	@Column(name = "comptype")
	private int compType;
	
	@Column(name = "bosscompid")
	private String bossCompId;
	

    @ManyToOne(cascade = CascadeType. REFRESH, targetEntity = Company.class  , fetch = FetchType.EAGER,optional = false) 
    @JoinColumn(name = "parentid",  unique = true, nullable = false, updatable = false ,insertable = true )
    @NotFound(action=NotFoundAction.IGNORE)
    @JsonIgnore
    @Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE })
    private  Company parentCompany; 
	 
	@Column(name = "opentype")
	private int openType;
	
	@Column(name = "orgcode")
	private String orgCode; 
	  
 	
	@OneToMany(cascade = CascadeType. REFRESH, mappedBy = "parentCompany", fetch = FetchType.EAGER)  
	@NotFound(action=NotFoundAction.IGNORE)
	@OrderBy("id")
	private List<Company> childCompany = new ArrayList<Company>();  

	public int getId() {
		return id;
	}

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

	public String getCompName() {
		return compName;
	}

	public void setCompName(String compName) {
		this.compName = compName;
	}

	public String getCompPhone() {
		return compPhone;
	}

	public void setCompPhone(String compPhone) {
		this.compPhone = compPhone;
	}

	public String getCompEmail() {
		return compEmail;
	}

	public void setCompEmail(String compEmail) {
		this.compEmail = compEmail;
	}

	public int getCompType() {
		return compType;
	}

	public void setCompType(int compType) {
		this.compType = compType;
	}

	public String getBossCompId() {
		return bossCompId;
	}

	public void setBossCompId(String bossCompId) {
		this.bossCompId = bossCompId;
	}
	
	

	 public Company getParentCompany() {
		return parentCompany;
	}

	public void setParentCompany(Company parentCompany) {
		this.parentCompany = parentCompany;
	}  
 
	public int getOpenType() {
		return openType;
	}

	public void setOpenType(int openType) {
		this.openType = openType;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	 
	public List<Company> getChildCompany() {
		return childCompany;
	}

	public void setChildCompany(List<Company> childCompany) {
		this.childCompany = childCompany;
	}

  @Override
	public String toString() {
		return "Company [id=" + id + ", compName=" + compName + ", compPhone=" + compPhone + ", compEmail=" + compEmail
				+ ", compType=" + compType + ", bossCompId=" + bossCompId 
				+ ", parentCompany=" + (parentCompany != null?parentCompany.toString():null ) 
				+ ", openType=" + openType + ", orgCode=" + orgCode
				//   + ", childCompany=" + (childCompany.size() > 0 ? childCompany.toString() :  null)
				 +"]";
	}  
  
}

 

 

 非级联删除 , 级联查询

 

package com.cmcc.cailing.service;

import java.util.List; 
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cmcc.cailing.entity.Company;

public class CompanyServiceTest {

	public static void main(String[] args) {
		  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  new String[] { "classpath*:applicationContext*.xml"  });   
		  CompanyService companyService = (CompanyService) context.getBean("companyService");
		  System.out.println(companyService);
		  
		  Company find2 = companyService.getCompanyById(2);
		  System.out.println("------------------");
		  System.out.println(find2.toString());
		  List<Company> list = find2.getChildCompany();
		  System.out.println(list.toString() );
		  System.out.println("------------------");
		  Company find3 = companyService.find("id", 2);
		  System.out.println(find3);
		  System.out.println("------------------");
		  Company find = companyService.find(1);
		  System.out.println(find.toString()); 
		  

			/* `id` int(11) NOT NULL AUTO_INCREMENT,
			  `compName` varchar(50) DEFAULT NULL,
			  `compPhone` varchar(20) DEFAULT NULL,
			  `compEmail` varchar(20) DEFAULT NULL,
			  `compType` int(11) DEFAULT NULL,
			  `bossCompId` varchar(255) DEFAULT NULL,
			  `parentId` int(11) NOT NULL,
			  `orgCode` varchar(100) DEFAULT NULL,
			  `openType` int(11) DEFAULT NULL,*/
		  Company entity = new Company();
		  entity.setCompName("renjiliankaidegognsi");
		  entity.setCompPhone("15011112222");
		  entity.setCompEmail("renhjilian@163.com");
		  entity.setCompType(0);
		  entity.setBossCompId(5+"");
		   Company company2 =  companyService.getCompanyById(2); 
		  entity.setParentCompany( company2);
		  entity.setOrgCode("1231232");
		  entity.setOpenType(0);
		  System.out.println("============================ save ");
		  companyService.save(entity );
		  System.out.println("============================ save end ,start find ");
		  Company company23 = companyService.getCompanyById(entity.getId());
		  System.out.println(company23.toString());
		  System.out.println("============================ find end, update start ");
		  company23.setCompName("12121212 renjilian ");
		  companyService.update(company23);
		  System.out.println("============================  update  end , delete start ");
//		  companyService.delete(entity);
		  companyService.delete(entity.getId()); 
		  System.out.println("============================  delete end ");
		  
	}
}

 

 



 
 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

目录
相关文章
|
8月前
|
Java 数据库连接
hibernate注解实体类(Dept.java)
hibernate注解实体类(Dept.java)
|
5月前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
177 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
5月前
|
API Java 数据库连接
从平凡到卓越:Hibernate Criteria API 让你的数据库查询瞬间高大上,彻底告别复杂SQL!
【8月更文挑战第31天】构建复杂查询是数据库应用开发中的常见需求。Hibernate 的 Criteria API 以其强大和灵活的特点,允许开发者以面向对象的方式构建查询逻辑,同时具备 SQL 的表达力。本文将介绍 Criteria API 的基本用法并通过示例展示其实际应用。此 API 通过 API 构建查询条件而非直接编写查询语句,提高了代码的可读性和安全性。无论是简单的条件过滤还是复杂的分页和连接查询,Criteria API 均能胜任,有助于提升开发效率和应用的健壮性。
184 0
|
5月前
|
数据库 开发者 Java
Hibernate映射注解的魔力:实体类配置的革命,让你的代码量瞬间蒸发!
【8月更文挑战第31天】Hibernate 是一款出色的对象关系映射框架,简化了 Java 应用与数据库的交互。其映射注解让实体类配置变得直观简洁。本文深入剖析核心概念与使用技巧,通过示例展示如何简化配置。
67 0
|
5月前
|
SQL Java 数据库连接
|
5月前
|
缓存 Java 数据库连接
什么是 Hibernate 查询语言或 HQL?
【8月更文挑战第21天】
162 0
|
5月前
|
SQL Java 数据库连接
在 Hibernate 中何时使用条件查询?
【8月更文挑战第21天】
63 0
|
5月前
|
缓存 Java 数据库连接
Hibernate 中的查询缓存是什么?
【8月更文挑战第21天】
49 0
|
5月前
|
SQL 安全 Java
|
7月前
|
JSON Java 数据库连接
Hibernate中使用@Lob 注解保存String[] 问题
Hibernate中使用@Lob 注解保存String[] 问题
48 2