---
title: "hibernate多对多中间表的保存问题"
author: "DLFSEEIT"
date: "2013-03-23T14:22:09Z"
tags: ["Hibernate", "中间表", "保存", "多对多"]
categories: ["企业架构"]
slug: "hibernate"
draft: false
lastmod: "2013-03-23T14:21:57Z"
id: 1835383
url: "https://dlfseeit.iteye.com/blog/1835383"
---

1. 表的设计
   a. 角色表
   b. 权限表
   c. 角色_权限表

2.代码 
 
```java

  RoleAuthority roleAuthority = new RoleAuthority();
					
  roleAuthority.getAuthority().setAuthorityId(authorityId);
  roleAuthority.getRole().setRoleId(roleId);
  this.saveOrUpdateEntity(roleAuthority);
 
```

以直报空指针错，发现roleAuthority.getAuthority()为空
然后想新new的对象的子对象怎么样才能不为空，只有在初始化的时候

```java

	/** default constructor */
	public YbRoleAuthority() {
		this.ybRole = new YbRole();
		this.ybAuthority = new YbAuthority();
	}

```

这样才能正常保存