Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Hibernate envers在使用Crudepository删除包含集合的实体时引发异常_Mysql_Hibernate_Spring Boot_Spring Data Jpa_Hibernate Envers - Fatal编程技术网

Mysql Hibernate envers在使用Crudepository删除包含集合的实体时引发异常

Mysql Hibernate envers在使用Crudepository删除包含集合的实体时引发异常,mysql,hibernate,spring-boot,spring-data-jpa,hibernate-envers,Mysql,Hibernate,Spring Boot,Spring Data Jpa,Hibernate Envers,我想知道是否有其他人在Spring boot应用程序中使用了hibernate envers和mysql,并使用接口Crudepository删除了具有集合的实体。我将一个示例应用程序与一个测试放在一起,该测试演示了生成的异常 该示例可以从 您需要mysql运行一个名为test_delete的数据库 CREATE DATABASE IF NOT EXISTS `test_delete` DEFAULT CHARACTER SET utf8; 运行测试 mvn test 您应该会看到以下异常:

我想知道是否有其他人在Spring boot应用程序中使用了hibernate envers和mysql,并使用接口Crudepository删除了具有集合的实体。我将一个示例应用程序与一个测试放在一起,该测试演示了生成的异常

该示例可以从

您需要mysql运行一个名为test_delete的数据库

CREATE DATABASE IF NOT EXISTS `test_delete` DEFAULT CHARACTER SET utf8;
运行测试

mvn test
您应该会看到以下异常:

    2015-08-11 21:36:28.725 ERROR 3855 --- [           main] org.hibernate.AssertionFailure           : HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.926 sec <<< FAILURE! - in com.dcp.test.AuthorRepositoryTest
testAuthorCRUD(com.dcp.test.AuthorRepositoryTest)  Time elapsed: 0.163 sec  <<< ERROR!
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:756)
    at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.java:75)
    at org.hibernate.event.spi.InitializeCollectionEvent.<init>(InitializeCollectionEvent.java:36)
    at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1931)
    at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
    at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:294)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:343)
    at org.hibernate.envers.internal.entities.mapper.relation.AbstractCollectionMapper.mapCollectionChanges(AbstractCollectionMapper.java:162)
    at org.hibernate.envers.internal.entities.mapper.relation.AbstractCollectionMapper.mapModifiedFlagsToMapFromEntity(AbstractCollectionMapper.java:212)
    at org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper.map(MultiPropertyMapper.java:105)
    at org.hibernate.envers.internal.synchronization.work.DelWorkUnit.generateData(DelWorkUnit.java:66)
    at org.hibernate.envers.internal.synchronization.work.AbstractAuditWorkUnit.perform(AbstractAuditWorkUnit.java:76)
    at org.hibernate.envers.internal.synchronization.AuditProcess.executeInSession(AuditProcess.java:119)
Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
@MappedSuperclass
@Audited
public abstract class BaseEntity implements Serializable {

    @GeneratedValue(generator = "guid")
    @GenericGenerator(name = "guid", strategy = "guid")
    @Column(columnDefinition = "CHAR(36)")
    @Id
    protected String id;

    @Temporal(TemporalType.TIMESTAMP)
    protected Date dateCreated = new Date();

    public String getId() {
        return id;
    }

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

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    private static final long serialVersionUID = -8371628134270930829L;
}
@Entity
@Audited
public class Author extends BaseEntity{

    private String email;

    private String name;

    @OneToMany(mappedBy = "author")
    private List<Book> books = new ArrayList<>();

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }


}
@Entity
@Audited
public class Book extends BaseEntity {

    private String title;

    private String genre;

    @ManyToOne
    private Author author;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

}
@Transactional
public interface AuthorRepository extends CrudRepository<Author, String> {

}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class AuthorRepositoryTest {

    @Autowired
    private AuthorRepository authorRepo;

    @Test
    public void testAuthorCRUD() {
        Author author = new Author();
        author.setName("Test Name");
        author.setEmail("test@mail.com");

        Author savedAuthor = authorRepo.save(author);

        Assert.assertNotNull(savedAuthor);

        authorRepo.delete(savedAuthor);
    }

}
BaseEntity.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
@MappedSuperclass
@Audited
public abstract class BaseEntity implements Serializable {

    @GeneratedValue(generator = "guid")
    @GenericGenerator(name = "guid", strategy = "guid")
    @Column(columnDefinition = "CHAR(36)")
    @Id
    protected String id;

    @Temporal(TemporalType.TIMESTAMP)
    protected Date dateCreated = new Date();

    public String getId() {
        return id;
    }

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

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    private static final long serialVersionUID = -8371628134270930829L;
}
@Entity
@Audited
public class Author extends BaseEntity{

    private String email;

    private String name;

    @OneToMany(mappedBy = "author")
    private List<Book> books = new ArrayList<>();

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }


}
@Entity
@Audited
public class Book extends BaseEntity {

    private String title;

    private String genre;

    @ManyToOne
    private Author author;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

}
@Transactional
public interface AuthorRepository extends CrudRepository<Author, String> {

}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class AuthorRepositoryTest {

    @Autowired
    private AuthorRepository authorRepo;

    @Test
    public void testAuthorCRUD() {
        Author author = new Author();
        author.setName("Test Name");
        author.setEmail("test@mail.com");

        Author savedAuthor = authorRepo.save(author);

        Assert.assertNotNull(savedAuthor);

        authorRepo.delete(savedAuthor);
    }

}
Author.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
@MappedSuperclass
@Audited
public abstract class BaseEntity implements Serializable {

    @GeneratedValue(generator = "guid")
    @GenericGenerator(name = "guid", strategy = "guid")
    @Column(columnDefinition = "CHAR(36)")
    @Id
    protected String id;

    @Temporal(TemporalType.TIMESTAMP)
    protected Date dateCreated = new Date();

    public String getId() {
        return id;
    }

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

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    private static final long serialVersionUID = -8371628134270930829L;
}
@Entity
@Audited
public class Author extends BaseEntity{

    private String email;

    private String name;

    @OneToMany(mappedBy = "author")
    private List<Book> books = new ArrayList<>();

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }


}
@Entity
@Audited
public class Book extends BaseEntity {

    private String title;

    private String genre;

    @ManyToOne
    private Author author;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

}
@Transactional
public interface AuthorRepository extends CrudRepository<Author, String> {

}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class AuthorRepositoryTest {

    @Autowired
    private AuthorRepository authorRepo;

    @Test
    public void testAuthorCRUD() {
        Author author = new Author();
        author.setName("Test Name");
        author.setEmail("test@mail.com");

        Author savedAuthor = authorRepo.save(author);

        Assert.assertNotNull(savedAuthor);

        authorRepo.delete(savedAuthor);
    }

}
AuthorRepository.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
@MappedSuperclass
@Audited
public abstract class BaseEntity implements Serializable {

    @GeneratedValue(generator = "guid")
    @GenericGenerator(name = "guid", strategy = "guid")
    @Column(columnDefinition = "CHAR(36)")
    @Id
    protected String id;

    @Temporal(TemporalType.TIMESTAMP)
    protected Date dateCreated = new Date();

    public String getId() {
        return id;
    }

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

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    private static final long serialVersionUID = -8371628134270930829L;
}
@Entity
@Audited
public class Author extends BaseEntity{

    private String email;

    private String name;

    @OneToMany(mappedBy = "author")
    private List<Book> books = new ArrayList<>();

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }


}
@Entity
@Audited
public class Book extends BaseEntity {

    private String title;

    private String genre;

    @ManyToOne
    private Author author;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

}
@Transactional
public interface AuthorRepository extends CrudRepository<Author, String> {

}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class AuthorRepositoryTest {

    @Autowired
    private AuthorRepository authorRepo;

    @Test
    public void testAuthorCRUD() {
        Author author = new Author();
        author.setName("Test Name");
        author.setEmail("test@mail.com");

        Author savedAuthor = authorRepo.save(author);

        Assert.assertNotNull(savedAuthor);

        authorRepo.delete(savedAuthor);
    }

}
如果您从EntityClasses手册中注释掉@Audited,则Author和BaseEntity可以很好地通过测试


有人知道如何使删除功能与envers一起工作吗?

我通过反复试验找到了两种方法,可以使删除操作按照问题中指定的方式工作

您可以指定要立即加载的集合

@OneToMany(mappedBy = "author", fetch = FetchType.EAGER)
也可以级联删除操作

@OneToMany(mappedBy = "author", cascade = CascadeType.REMOVE)

这是使测试按照问题中的规定通过所需的最低配置。

我通过反复试验找到了两种方法使删除操作按照问题中的规定工作

您可以指定要立即加载的集合

@OneToMany(mappedBy = "author", fetch = FetchType.EAGER)
也可以级联删除操作

@OneToMany(mappedBy = "author", cascade = CascadeType.REMOVE)

这是您通过问题中指定的测试所需的最低配置。

也有相同的问题,通过对相关关系进行显式连接来修复,而且我认为首先删除子关系也是一件常见的事情,因此您可能无论如何都必须这样做


我认为在envers的环境中,您必须使用repository delete方法才能在删除时保存修订。

也有同样的问题,通过对所讨论的关系进行显式连接来修复,而且我认为首先删除子关系也是一件常见的事情,因此您可能无论如何都必须这样做

我认为在envers环境中,您必须使用repositorydelete方法才能在delete上保存修订