jpa@OneToOne关联关系

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)