关于Spring的CrudRepository的一对多的save问题
澳门银河官网
澳门银河官网
当前位置 : 澳门银河官网 > 澳门银河官网网址

关于Spring的CrudRepository的一对多的save问题

各位高手,我在使用hibernate、jpa、springdata集成时遇到了如下的问题:

model

@Entity
@Tablename = "t_group", schema = "domain"
public class Group
{
    @Id
    @GeneratedValue
    private Long id;
    
    private String name;
    
    @OneToManymappedBy = "group", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true
    private Set<User> users;

    public String getName
    {
        return name;
    }

    public void setNameString name
    {
        this.name = name;
    }

    public Set<User> getUsers
    {
        return users;
    }

    public void setUsersSet<User> users
    {
        this.users = users;
    }

    public Long getId
    {
        return id;
    }   
}
@Entity
@Tablename = "t_user", schema = "domain"
public class User
{
    @Id
    @GeneratedValue
    private Long id;
    
    private String name;
    
    @ManyToOnefetch = FetchType.LAZY
    @JoinColumnname = "group_id", nullable = false
    private Group group;

    public String getName
    {
        return name;
    }

    public void setNameString name
    {
        this.name = name;
    }
    
    public Long getId
    {
        return id;
    }   
}

repository

public interface GroupRepository extends CrudRepository<Group, Long>
{

}

config

@Configuration
@EnableJpaRepositoriesbasePackages = { "**.**.**.repository" }
public class RepositoryIntegrationTestsConfig
{
    @Bean
    public PlatformTransactionManager transactionManagerEntityManagerFactory emf
    {
        JpaTransactionManager transactionManager = new JpaTransactionManager;
        transactionManager.setEntityManagerFactoryemf;

        return transactionManager;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryDataSource dataSource
    {
        LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean;
        emfb.setDataSourcedataSource;
        emfb.setJpaVendorAdapterjpaVendorAdapter;
        emfb.setPackagesToScan"com.**.**.entity";

        Properties properties = new Properties;
        properties.setProperty"hibernate.hbm2ddl.auto", "create";
        properties.setProperty"hibernate.format_sql", "true";
        emfb.setJpaPropertiesproperties;

        return emfb;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter
    {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter;
        adapter.setDatabaseDatabase.H2;
        adapter.setShowSqlfalse;
        adapter.setGenerateDdlfalse;
        adapter.setDatabasePlatform"org.hibernate.dialect.H2Dialect";

        return adapter;
    }

    @Beanname = "integrationTestDataSource"
    public DataSource dataSource
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource;
        dataSource.setDriverClassName"org.h2.Driver";
        dataSource.setUrl"jdbc:h2:~/test;INIT=CREATE SCHEMA IF NOT EXISTS domain";
        dataSource.setUsername"sa";
        dataSource.setPassword"";

        return dataSource;
    }
}

test

@RunWithSpringJUnit4ClassRunner.class
@ContextConfigurationclasses = RepositoryIntegrationTestsConfig.class
public class GroupRepositoryIntegrationTest
{
    @Autowired
    private GroupRepository repository;
    
    @Test
    public void saveShouldSucceed
    {
        Group group = new Group;
        group.setName"name";
        
        Set<User> users = new HashSet<>;
        User user1 = new User;    
        user1.setName"user1";
        
        User user2 = new User;
        user2.setName"user2";
        
        users.adduser1;
        users.adduser2;

        group.setUsersusers;
        repository.savegroup;
    }
}

当我执行测试时,会报如下错误:

...
Caused by: org.h2.jdbc.jdbcSQLException: NULL not allowed for column "GROUP_ID";
...

请问是什么原因?

用了双向关联,user必须调用setGroup方法

@JoinColumnname = "role_id", nullable = false
nullable默认值:true。
默认情况下,JPA 持续性提供程序假设允许所有列包含空值。
如果不允许该列包含空值,请将 nullable 设置为 false。

没有实体名称为role,所以找不到role_id。如果避免这个报错的话,nullable修改为true

估计是这句的原因 @JoinColumnname = "role_id", nullable = false
可以通过 UserRepository 尝试去保存一下

广告位

澳门银河官网