@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("[email protected]");
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捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!