ManyToMany关系:数据不在DB中持久存在

时间:2016-03-13 20:57:24

标签: java hibernate jpa many-to-many

从UI获取值后,能够从DTO中提取值并分配给相应的字段。 创建完整的用户对象后,当尝试在db中保留此用户对象时,这些值不会在数据库中持久存在。

无法找到错误的地方,有人可以帮助我。 感谢。

ControllerFile.java

public static String doubleVowel(String str) { 
    StringBuilder result = new StringBuilder();
    for(int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        result.append(ch);
        if (ch == 'a' || 
            ch == 'e' ||
            ch == 'i' || 
            ch == 'o' ||
            ch == 'u') {
            result.append(ch); // second append for vowels
    {
    return result.toString();
}

DaoImplFile.java

public String createNewUser(Model model,@ModelAttribute("UserRoleDTO") UserRoleDTO userRoleDTO) {
        String message = null;
        User user = null;
        List<Role> roles = new ArrayList<Role>();
            user = userRoleDTO.getUser();
            for(Long r:userRoleDTO.getRole()) {
                Role role = new Role();
                role.setRoleId(r);
                roles.add(role);    
            }
            user.setRoles(roles);
            loginService.createUser(user);
}

User.java

@Repository
@Transactional
public class DaoImplFile implements LoginDao {

    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public String createNewUser(User user) {
        String message = null;
        try{
            entityManager.persist(user);
            message = "User info has been submitted successfully";
        }catch(Exception e){
            message = "Sorry !!!! User info has not been submitted successfully";
        }
        return message;
    }
}

Role.java

@Entity
@Table(name="user")
public class User implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="userId")
    private Long userId;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "userrole",joinColumns = { 
            @JoinColumn(name = "UserId", nullable = false, updatable = false) }, 
            inverseJoinColumns = { @JoinColumn(name = "roleId", 
                    nullable = false, updatable = false) })
    private Set<Role> roles;

    }

2 个答案:

答案 0 :(得分:2)

用户和角色之间存在双向关系,但您只设置了一个方向。这可能会导致意外行为。

将createNewUser更改为:

public String createNewUser(Model model,@ModelAttribute("UserRoleDTO") UserRoleDTO userRoleDTO) {
    String message = null;
    User user = null;
    List<Role> roles = new ArrayList<Role>();
        user = userRoleDTO.getUser();
        for(Long r:userRoleDTO.getRole()) {
            Role role = new Role();
            role.setRoleId(r);
            List<User> users = new ArrayList<>();
            users.add(user);
            role.setUser(users);
            roles.add(role);    
        }
        user.setRoles(roles);
        loginService.createUser(user);
}

在添加角色之前可能需要保存用户,但我不完全确定。

答案 1 :(得分:0)

我已经在USER类中进行了更改,这对我有用......

CascadeType.ALL 至 CascadeType.MERGE

感谢@ mh-dev,解释了解决同样问题的方法,我没有尝试过,觉得应该也有效。