gpt4 book ai didi

java - 在 @ManyToMany 映射中,未将数据插入数据库(空指针异常)

转载 作者:行者123 更新时间:2023-12-02 01:38:20 26 4
gpt4 key购买 nike

我是 Jpa 和 hibernate 的新手。我尝试在 jpa 中使用 @ManytoMany 映射并将该类用作:

用户.java 包 com.jpa.manytomany.manytomany.model;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

@Entity
public class User {

@Id
@GeneratedValue
private Long id;


@NotNull
@Size(max = 100)
@Column(unique = true)
private String userName;

@NotNull
@Size(max=100)
@Column(unique = true)
private String password;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name="user_role",joinColumns=@JoinColumn(name="user_id"),
inverseJoinColumns = @JoinColumn(name="role_id"))
private List<Role> roles;


public User() {
}

public User(String userName, String password) {

this.userName = userName;
this.password = password;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public List<Role> getRoles() {
return roles;
}

public void setRoles(Role role) {
this.roles.add(role);
}
}

角色.java

package com.jpa.manytomany.manytomany.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Role {

@Id
@GeneratedValue
private Long id;

@NotNull
private String roleName;

@ManyToMany(mappedBy = "roles")
private List<User> users=new ArrayList<>();

public Role() {
}

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;
}

public Role(String roleName) {
this.roleName = roleName;
}

public List<User> getUsers() {
return users;
}

public void setUsers(User user) {
this.users.add(user);
}
}

我使用了 hibernate 的自动表生成器属性,它生成为:

enter image description here

我编写了一个测试用例,将数据插入到这三个表中,如下所示:

package com.jpa.manytomany.manytomany;

import com.jpa.manytomany.manytomany.model.Role;
import com.jpa.manytomany.manytomany.model.User;
import com.jpa.manytomany.manytomany.repository.RoleRepository;
import com.jpa.manytomany.manytomany.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ManytomanyApplicationTests {

private Logger logger= LoggerFactory.getLogger(this.getClass());

@Autowired
private RoleRepository roleRepository;

@Autowired
private UserRepository userRepository;

@Test
public void insert_into_three_tables() {
User user=new User("Ashwin","password1");

Role role=new Role("ROLE_ADMIN");

userRepository.insertUserAndRole(user,role);
}

}

用户存储库是:

package com.jpa.manytomany.manytomany.repository;

import com.jpa.manytomany.manytomany.model.Role;
import com.jpa.manytomany.manytomany.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

@Repository
@Transactional
public class UserRepository {

private Logger logger= LoggerFactory.getLogger(this.getClass());

@Autowired
private EntityManager entityManager;



public void insertUserAndRole(User user, Role role) {
user.setRoles(role);
role.setUsers(user);

entityManager.persist(user);
entityManager.persist(role);
}
}

角色存储库是:

package com.jpa.manytomany.manytomany.repository;

import com.jpa.manytomany.manytomany.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

@Repository
@Transactional
public class RoleRepository {

@Autowired
private EntityManager em;


public Role insertRole(Role role){
em.persist(role);
return role;
}
}

我收到的错误是:

java.lang.NullPointerException
at com.jpa.manytomany.manytomany.model.User.setRoles(User.java:70)
at com.jpa.manytomany.manytomany.repository.UserRepository.insertUserAndRole(UserRepository.java:29)
at com.jpa.manytomany.manytomany.repository.UserRepository$$FastClassBySpringCGLIB$$63f7afd9.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.jpa.manytomany.manytomany.repository.UserRepository$$EnhancerBySpringCGLIB$$2a8fc203.insertUserAndRole(<generated>)
at com.jpa.manytomany.manytomany.ManytomanyApplicationTests.insert_into_three_tables(ManytomanyApplicationTests.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

最佳答案

角色需要使用列表进行初始化

private List<Role> roles= new ArrayList<Role>();

关于java - 在 @ManyToMany 映射中,未将数据插入数据库(空指针异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57504004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com