当前位置:首页 > 开发 > 开源软件 > 正文

jpa@OneToOne关联关系

发表于: 2014-12-20   作者:布衣凌宇   来源:转载   浏览:
jpa
摘要: Nruser里的pruserid关联到Pruser的主键id,实现对一个表的增删改,另一个表的数据随之增删改。 Nruser实体类 //***************************************************************** @Entity @Table(name="nruser") @DynamicInsert @Dynam
Nruser里的pruserid关联到Pruser的主键id,实现对一个表的增删改,另一个表的数据随之增删改。
Nruser实体类
//*****************************************************************
@Entity
@Table(name="nruser")
@DynamicInsert @DynamicUpdate
public class Nruser {
private Long id;
private String name;[align=center][/align]
private String mobile;
private String email;
private Pruser pruser;
public Nruser(){
super();
}

public Nruser(Long id){
this();
this.id = id;
}

@OneToOne(cascade=CascadeType.ALL,optional=false) //optional=false表明Pruser不得为空
@JoinColumn(name="pruserid",referencedColumnName="id",unique=true) //unique=true表明pruserid列的值不可重复
public Pruser getPruser() {
return pruser;
}
public void setPruser(Pruser pruser) {
this.pruser = pruser;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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 String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

}
//******************************************************************
Pruser实体类
@Entity
@Table(name = "aaa")
@DynamicInsert @DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Pruser {
private Long id;
private String name;
private Integer age;
private String email;
private String mobile;
private Nruser nruser;


public Pruser(){
super();
}

public Pruser(Long id){
this();
this.id = id;
}
// mappedBy="pruser" 指明Pruser作为双向关系的维护段,负责外键的更新,起主导作用
@OneToOne(optional=true,cascade=CascadeType.ALL,mappedBy="pruser")
public Nruser getNruser() {
return nruser;
}

public void setNruser(Nruser nruser) {
this.nruser = nruser;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}
}
//******************************************************************
Nruser Controller

@Controller
@RequestMapping(value="${adminPath}/sword/nruser")
public class NruserController extends BaseController {
@Autowired
private NruserService nruserService;

@ModelAttribute("nruser")
public Nruser get(@RequestParam(required=false) Long id){
if(id!=null){
return nruserService.findOneById(id);
}else{
return new Nruser();
}
}
@RequestMapping(value={"lsit",""})
public String list(Nruser nruser,HttpServletRequest request,HttpServletResponse response,Model model){
Page<Nruser> page=nruserService.getAll(new Page<Nruser>(request,response),nruser);
model.addAttribute("page", page);
return "modules/nruser/nruserList";
}
@RequestMapping(value="form")
public String form(Nruser nruser,Model model){
model.addAttribute("nruser", nruser);
return "modules/nruser/nruserForm";
}
@RequestMapping(value="save")
public String save(Nruser nruser,RedirectAttributes redirectAttributes){
if(nruser.getPruser()==null){
Pruser pruser=new Pruser();
pruser.setMobile(nruser.getMobile());
pruser.setName(nruser.getName());
pruser.setEmail(nruser.getEmail());
nruser.setPruser(pruser);
nruserService.save(nruser);
}else{
Pruser currentPruser=nruser.getPruser();
currentPruser.setMobile(nruser.getMobile());
currentPruser.setName(nruser.getName());
currentPruser.setEmail(nruser.getEmail());
nruser.setPruser(currentPruser);
nruserService.save(nruser);
}

addMessage(redirectAttributes, "保存'"+nruser.getName()+"'成功");
return "redirect:"+Global.getAdminPath()+"/sword/nruser/?repage";
}
@RequestMapping(value="delete")
public String delete(Long id,RedirectAttributes redirectAttributes){
String name=nruserService.findOneById(id).getName();
nruserService.delete(id);
addMessage(redirectAttributes, "删除'"+name+"'成功");
return "redirect:"+Global.getAdminPath()+"/sword/nruser/?repage";
}
}
//******************************************************************
Pruser Controller

@Controller
@RequestMapping(value = "${adminPath}/sys/pruser")  //实体类pruser 路径
public class PruserController extends BaseController {

@Autowired
private PruserService pruserService;
/*
* 通过id查询单条数据
*/
@ModelAttribute("pruser")
public Pruser get(@RequestParam(required=false) Long id) {
if (id != null){
return pruserService.getPruser(id);
}else{
return new Pruser();
}
}
/*
* 查询全部数据
*/

@RequestMapping(value = {"list", ""})
public String list(Pruser pruser, HttpServletRequest request, HttpServletResponse response, Model model) {
Page<Pruser> page = pruserService.findPruser(new Page<Pruser>(request, response), pruser);
    model.addAttribute("page", page);
    return "financial/pruser/pruserList";
}

/*
* 新增数据
*/
@RequestMapping(value = "save")
public String save(Pruser pruser){
if(pruser.getNruser()==null){
Nruser nruser=new Nruser();
nruser.setName(pruser.getName());
nruser.setMobile(pruser.getMobile());
nruser.setEmail(pruser.getEmail());
nruser.setPruser(pruser);
pruser.setNruser(nruser);
pruserService.save(pruser);
}else{
Nruser currentNruser=pruser.getNruser();
currentNruser.setName(pruser.getName());
currentNruser.setMobile(pruser.getMobile());
currentNruser.setEmail(pruser.getEmail());
currentNruser.setPruser(pruser);
pruser.setNruser(currentNruser);
pruserService.save(pruser);
}
return "redirect:"+Global.getAdminPath()+"/sys/pruser/?repage";
}

@RequestMapping(value = "form")
public String form(Pruser pruser, Model model) {
model.addAttribute("pruser", pruser);
return "modules/sys/pruserForm";
}
/*
* 通过id删除数据
*/

@RequestMapping(value = "delete")
public String delete(long id,RedirectAttributes redirectAttributes) {
pruserService.deletePruser(id);
addMessage(redirectAttributes, "删除用户成功");
return "redirect:"+Global.getAdminPath()+"/sys/pruser/?repage";
}
@RequestMapping(value="upload",method=RequestMethod.POST)
public String upload(Model model,HttpServletRequest request,RedirectAttributes redirectAttributes,@RequestParam(value="myfile") MultipartFile multipartFile) throws IllegalStateException, IOException{
String path=request.getSession().getServletContext().getRealPath("upload");//获取上传的文件保存的路径
String filename=multipartFile.getOriginalFilename();//获取上传文件的名称
File targetFile=new File(path,filename);
if(filename.equals("")){
addMessage(redirectAttributes, "请选择上传文件");
return "redirect:"+Global.getAdminPath()+"/sys/pruser/";
}
if(!targetFile.exists()){
targetFile.mkdirs();

}else{
addMessage(redirectAttributes, "文件已存在");
return "redirect:"+Global.getAdminPath()+"/sys/pruser/";
}
multipartFile.transferTo(targetFile);
addMessage(redirectAttributes, "上传"+filename+"成功");
// model.addAttribute("fileUrl",request.getContextPath()+"/upload/"+filename);
return "redirect:"+Global.getAdminPath()+"/sys/pruser/";
}
}
//******************************************************************

jpa@OneToOne关联关系

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
UML中的关联关系其内在意思就是has a 如图: 相对于依赖关系,关联关系在代码中有所体现.上图中的关联
UML中的关联关系其内在意思就是has a 如图: 相对于依赖关系,关联关系在代码中有所体现.上图中的关联
你和你的心脏之间是composition关系 你和你买的书之间是aggregation关系 你和你的朋友之间是associa
共享主键关系 两个关联表使用相同的主键值 类: <!--通过外键和user的id共享 --> <id name
UML中的关联关系其内在意思就是has a 如图: 相对于依赖关系,关联关系在代码中有所体现.上图中的关联
Hibenrnate的关联关系是指不同持久类之间的一种结果关系,简单地说,关联关系描述某个对象在某一段
  有这样两个持久化对象Person和Book,两者是一对多双向关联关系,对Person.hbm.xml的bag元素设置
初学 hibernate 时是被 hibernate 中的复杂的关系搞得晕头转向。 一对多 ,多对一,多对多。单向,双
  Hibernate对于持久化实体间的关联关系解除,有两种实现方式,一种是将关联关系实体中彻底删除,
类间关系 在类图中,除了需要描述单独的类的名称、属性和操作外,我们还需要描述类之间的联系,因为
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号