Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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
Java 休眠删除不';不执行_Java_Hibernate_Spring Boot - Fatal编程技术网

Java 休眠删除不';不执行

Java 休眠删除不';不执行,java,hibernate,spring-boot,Java,Hibernate,Spring Boot,我正在使用一个测试spring引导项目来学习一些hibernate。我有两个接口扩展了crudepository-Address和Student,都用@Transactional和@Repository注释。许多学生应该共用同一个地址 @Transactional @Repository public interface AddressDao extends CrudRepository<Address, Integer> {} @Entity public class Addr

我正在使用一个测试spring引导项目来学习一些hibernate。我有两个接口扩展了crudepository-Address和Student,都用@Transactional和@Repository注释。许多学生应该共用同一个地址

@Transactional
@Repository
public interface AddressDao extends CrudRepository<Address, Integer> {}


@Entity
public class Address {

@Id
@GeneratedValue
private int addressId;
private String street;
private String city;
private String state;
private String zipcode;
等等。 主要课程包括:

@SpringBootApplication
public class MainSpringBootClass extends SpringBootServletInitializer implements CommandLineRunner {

@Autowired
StudentDao studentDao;

@Autowired
AddressDao addressDao;

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


@Override
@Transactional
public void run(String... args) {

    List<Address> list = new ArrayList<>();
    addressDao.findAll().forEach(list::add);

    Address address;

    if (list.size() > 0) {
        address = list.get(0);
    } else {
        address = new Address().withCity("London").withZipcode("W1J 9LL")
                .withStreet("Piccadilly").withState("UK");
    }

    Student student1 = new Student().withName("Ann").withAddress(address);
    Student student2 = new Student().withName("James").withAddress(address);

    studentDao.save(student1);
    studentDao.save(student2);

    addressDao.delete(1);
    addressDao.findAll().forEach(System.out::println);
}
@springboot应用程序
公共类MainSpringBootClass扩展SpringBootServletializer实现CommandLineRunner{
@自动连线
学生道学生道;
@自动连线
地址道地址道;
公共静态void main(字符串[]args){
run(MainSpringBootClass.class,args);
}
@凌驾
@交易的
公共无效运行(字符串…参数){
列表=新的ArrayList();
addressDao.findAll().forEach(列表::添加);
地址;
如果(list.size()>0){
地址=list.get(0);
}否则{
地址=新地址()。带城市(“伦敦”)。带Zipcode(“W1J 9LL”)
.威斯特莱特(“皮卡迪利”)。威斯特莱特(“英国”);
}
Student student1=新学生()。带姓名(“Ann”)。带地址(地址);
Student student2=新学生()。带姓名(“詹姆斯”)。带地址(地址);
studentDao.save(student1);
studentDao.save(student2);
删除第(1)款;
addressDao.findAll().forEach(System.out::println);
}
}

问题是每次我运行这段代码时,都会添加2个新学生,但我看不到addressDao.delete(1)正在执行,不会引发异常,不会打印hibernate的日志,因为它试图删除记录。此外,事务也不会回滚。 如果从Student.java中删除(cascade=CascadeType.ALL),我可以看到addressDao.delete(1)和exception,但这也意味着在保存Student之前必须先手动保存地址。 我做错了什么

编辑: 我补充说 长距离移动城市(字符串城市); 到我的AddressDao并替换AddressDao。用它删除(1)。我看到它返回1,所以看起来它被执行了,但我仍然没有得到任何异常。
只有在不使用save之前,我才会遇到异常。

您能分享您的AddressDao实现吗?我编辑了这个问题。StudentDao看起来是一样的,只是它使用长而不是整数。每个实体上都有getter和setter吗?如果不是的话,我相信是有问题的。还有,正如黄俊邦所说的,你能分享一下你刀和酒吗?也许还有一个问题。是的,我有getter/setter,为了简洁起见没有包括它们。Crudepository来自org.springframework.data.repository。请问您为什么在地址和学生DAO中使用“@Transactional”和“@repository”而不是@RepositoryRestResource?也许通过改变这一点,问题就解决了。
@SpringBootApplication
public class MainSpringBootClass extends SpringBootServletInitializer implements CommandLineRunner {

@Autowired
StudentDao studentDao;

@Autowired
AddressDao addressDao;

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


@Override
@Transactional
public void run(String... args) {

    List<Address> list = new ArrayList<>();
    addressDao.findAll().forEach(list::add);

    Address address;

    if (list.size() > 0) {
        address = list.get(0);
    } else {
        address = new Address().withCity("London").withZipcode("W1J 9LL")
                .withStreet("Piccadilly").withState("UK");
    }

    Student student1 = new Student().withName("Ann").withAddress(address);
    Student student2 = new Student().withName("James").withAddress(address);

    studentDao.save(student1);
    studentDao.save(student2);

    addressDao.delete(1);
    addressDao.findAll().forEach(System.out::println);
}