多对多存数据时,中间表没有数据

//action里的add方法
public String add(){
    model.getRoles().addAll(roleService.getByIds(roleIds));
    System.out.println(roleIds[0]);//能正确输出值
    System.out.println(model.getRoles());//能正确得到对象

    userService.add((User) model);
    return "_list";
}

package oa0106.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**

  • 用户
  • */
    @Entity
    @Table(name="t_user")
    public class User {

    private long id;
    private String name;
    private String password;
    private String gender;
    private String loginName;
    private String email;
    private int phoneNumber;
    private String describe;
    private Set roles=new HashSet();
    private Department department;
    @Id
    @GeneratedValue
    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 getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public String getGender() {
    return gender;
    }
    public void setGender(String gender) {
    this.gender = gender;
    }
    public String getLoginName() {
    return loginName;
    }
    public void setLoginName(String loginName) {
    this.loginName = loginName;
    }
    public String getEmail() {
    return email;
    }
    public void setEmail(String email) {
    this.email = email;
    }
    public int getPhoneNumber() {
    return phoneNumber;
    }
    public void setPhoneNumber(int phoneNumber) {
    this.phoneNumber = phoneNumber;
    }
    @Column(name="_describe")
    public String getDescribe() {
    return describe;
    }
    public void setDescribe(String describe) {
    this.describe = describe;
    }
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="role_user",
    joinColumns={@JoinColumn(name="user_id")},
    inverseJoinColumns={@JoinColumn(name="role_id")})
    public Set getRoles() {
    return roles;
    }
    public void setRoles(Set roles) {
    this.roles = roles;
    }
    @ManyToOne()
    @JoinColumn(name="department_id")
    public Department getDepartment() {
    return department;
    }
    public void setDepartment(Department department) {
    this.department = department;
    }
    }

package oa0106.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**

  • 职位
  • */ @Entity @Table(name="t_role") public class Role { private long id; private String roleName; private String describe; private Set users=new HashSet(); @Id @GeneratedValue public long getId() { return id; } public void setId(long id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } @Column(name="_describe") public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } @ManyToMany(mappedBy="roles",cascade=CascadeType.ALL) public Set getUsers() { return users; } public void setUsers(Set users) { this.users = users; } }

//dao里的存储方法
public void save(T entity) {
hibernateTemplate.save(entity);
}

不知道是不是hibernateTemplate的问题,用户和职位关联不上,其他的数据都能存进数据库,就是用户和职位的中间表没有数据。
查看全部
u010945068
Hot_Doge
2015/01/14 18:56
  • 中间表没数据
  • many o any
  • hibernate
  • 点赞
  • 收藏
  • 回答
    私信

1个回复