【问题标题】:Cascade on bidirectional @OneToOne with @MapsId使用@MapsId 在双向@OneToOne 上级联
【发布时间】:2021-01-23 17:15:25
【问题描述】:

我在 Spring Boot 2.1.1.RELEASE 工作,休眠 5.3.7.FINAL

规则是,用户可以没有电话(phoneuser 中可以为空)但是,电话不能在没有用户的情况下存在(userphone 中不为空)。

实体:

@Entity
public class Phone {
    
   @Id
   private Long id;
    
   @OneToOne
   @MapsId
   @JoinColumn(name = "id")
   private User user;
    
   public Long getId() {
       return id;
   }
   public void setId(final Long id) {
       this.id = id;
   }
    
   public User getUser() {
      return user;
   }
   public void setUser(final User user) {
      this.user = user;
   }
}
        
@Entity
public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
    
   @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
   private Phone phone;
    
   public Long getId() {
       return id;
   }
   public void setId(final Long id) {
       this.id = id;
   }
    
   public Phone getPhone() {
       return phone;
   }
   public void setPhone(final Phone phone) {
       this.phone = phone;
   }
}

控制器:

@RestController
@RequestMapping
public class UserController {
    
   private final UserService userService;
    
   public UserController(final UserService userService) {
       this.userService = userService;
   }
    
   @GetMapping("/demo")
   public void createUserAndAddPhone() {
       final User user = new User();
       userService.save(user);
       final Phone phone = new Phone();
    
       phone.setUser(user);
       user.setPhone(phone);
       userService.update(user);
   }
}

存储库:

public interface UserRepository  extends PagingAndSortingRepository<User, Long> {
}

服务:

@Service
public class UserService {
    
   private final UserRepository userRepository;
    
   public UserService(final UserRepository userRepository) {
       this.userRepository = userRepository;
   }
    
   @Transactional
   public void save(final User user) {
       userRepository.save(user);
   }
    
   @Transactional
   public void update(final User user) {
       userRepository.save(user);
   }
}

表格:

CREATE TABLE `phone` (
   `id` bigint(20) NOT NULL,
   PRIMARY KEY (`id`)
);
    
CREATE TABLE `user` (
   `id` bigint(20) NOT NULL AUTO_INCREMENT,
   PRIMARY KEY (`id`)
)

应用 yml:

spring:
   datasource:
      url: jdbc:mysql://localhost:3308/demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true
      username: root
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
   jpa:
      database-platform: org.hibernate.dialect.MySQL5Dialect
      hibernate:
         ddl-auto: validate

pom xml:

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>jsr305</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

致电GET http://localhost:8080/demo 给我一个错误:

org.hibernate.id.IdentifierGenerationException: 试图分配 id 从 null 一对一属性 [com.example.demo.Phone.user]

当我注释掉 userService.save(user); 时,它会起作用并生成:

insert into `user` 
values ( )

-- Generated identifier: 13, using strategy: org.hibernate.id.ForeignGenerator
                
insert into `phone` (`id`)  values (?)
-- Binding parameter [1] as [BIGINT] - [13]

但如果 user 被持久化然后更新,它就不起作用(引发上述异常)

【问题讨论】:

  • 您确定在事务中运行代码吗?我试过你的映射,它对我来说很好。
  • 是的,有org.springframework.transaction.annotation.Transactional注释
  • 请出示您的application.properties
  • 我从文件中添加了弹簧部分。够了吗? (只不过是一些石英和电子邮件配置,所以我跳过了其余部分)我在 docker 上有 mysql,因此端口不同。
  • @SternK 你认为这可能是一个休眠错误吗? [下面的链接](版本匹配)。你说它对你有用,你有什么休眠版本? stackoverflow.com/questions/58561156/…

标签: java hibernate jpa one-to-one


【解决方案1】:

啊,最后,这是一个休眠错误(HHH-12436)。

它可以通过以下用例在纯休眠应用程序中重现:

Session session = sessionFactory.openSession();

Transaction tr1 = session.beginTransaction();
User user = new User();
session.persist(user);
tr1.commit();

Transaction tr2 = session.beginTransaction();
Phone newPhone = new Phone();
user.setPhone(newPhone);
newPhone.setUser(user);
session.merge(user);
tr2.commit();

session.close();

从上面的链接可以看出,它已在 hibernate 5.4 分支中修复。

附:我能够在最新的 5.3 版本中重现该问题 (5.3.18.Final)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2017-09-18
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2020-06-21
    相关资源
    最近更新 更多