Java Hibernate复合外键失败,生成的Id为

Java Hibernate复合外键失败,生成的Id为,java,mysql,hibernate,jpa,spring-data-jpa,Java,Mysql,Hibernate,Jpa,Spring Data Jpa,我创建了一个简约示例程序,展示了我的问题: 我的国家有n个所有者(1到n), 主人有一只狗(1对1), 所有不动产都是识别关系。 我正在使用MySql,我已经设置了“spring.jpa.hibernate.ddl-auto=create” 在我的application.properties文件中,因此数据库表将自动生成 国家: @实体 @表(name=“country”) @资料 公营国家{ @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) 私人

我创建了一个简约示例程序,展示了我的问题:
我的国家有n个所有者(1到n),
主人有一只狗(1对1),
所有不动产都是识别关系。
我正在使用MySql,我已经设置了“spring.jpa.hibernate.ddl-auto=create”
在我的application.properties文件中,因此数据库表将自动生成

国家:

@实体
@表(name=“country”)
@资料
公营国家{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@纵队
私有字符串名称;
@OneToMany(mappedBy=“countryId”,cascade=CascadeType.ALL)
私人收藏品所有者;
公共国家(){
}
}  
所有者:

@实体
@表(name=“owner”)
@IdClass(OwnerId.class)
@资料
公共类所有者{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“id”)
私人长id;
@身份证
@JoinColumn(name=“country\u id”)
@列(name=“country\u id”)
私人长countryId;
@纵队
私有字符串名称;
@OneTONE(mappedBy=“owner”,cascade=CascadeType.ALL)
私家狗;
公共所有者(){
}
}  
狗:

@实体
@资料
@表(name=“dog”)
@IdClass(DogId.class)
公家犬{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@纵队
私有字符串名称;
@奥内托内
@连接柱({
@JoinColumn(name=“owner\u id”,referencedColumnName=“id”,insertable=false,updateable=false),
@JoinColumn(name=“owner\u country\u id”,referencedColumnName=“country\u id”,insertable=false,updateable=false)
})
@杰索尼奥雷
私人业主;
@身份证
@列(name=“owner\u id”)
私人长所有者id;
@身份证
@列(name=“所有者\国家\ id”)
私人长期所有者_国家_id;
公犬(){
}
}
idclass:

@数据
公共类DogId实现可序列化的{
私人长id;
私人长所有者id;
私人长期所有者_国家_id;
公共DogId(长id、长所有者\u id、长所有者\u国家\u id){
this.id=id;
this.owner\u id=owner\u id;
this.owner\u country\u id=owner\u country\u id;
}
公共DogId(){
}
公共布尔等于(对象o){
if(this==o){
返回true;
}
如果(o==null | | getClass()!=o.getClass()){
返回false;
}
DogId pk=(DogId)o;
返回Objects.equals(id,pk.id)&&
Objects.equals(owner\u id,pk.owner\u id)&&
Objects.equals(所有者\国家\ id,pk.owner \国家\ id);
}
@凌驾
公共int hashCode(){
返回Objects.hash(id,owner\u id,owner\u country\u id);
}
@资料
公共类OwnerId实现可序列化的{
私人长id;
私人长countryId;
公有id(长id、长countryId){
this.id=id;
this.countryId=countryId;
}
公有ID(){
}
公共布尔等于(对象o){
if(this==o){
返回true;
}
如果(o==null | | getClass()!=o.getClass()){
返回false;
}
OwnerId pk=(OwnerId)o;
返回Objects.equals(id,pk.id)&&
Objects.equals(countryId,pk.countryId);
}
@凌驾
公共int hashCode(){
返回Objects.hash(id,countryId);
}
REST控制器:

@org.springframework.web.bind.annotation.RestController
@请求映射(value=“/api/country/{countryId}/owner”)
公共类所有者控制器{
私人所有人住所所有人住所;
@自动连线
公共所有者控制器(所有者存储所有者存储){
this.ownerRepository=ownerRepository;
}
@后期映射(value=“/add”)
public ResponseEntity addOwner(@RequestBody-Owner-Owner,@PathVariable(value=“countryId”)long countryId){
所有者。setCountryId(countryId);
owner.getDog().setOwner\u country\u id(countryId);
Owner dbOwner=ownerRepository.save(所有者);
//简单的本机sql更新查询
dogRepository.UpdatedOwnerId(dbOwner.getId(),
dbOwner.getDog().getId());
返回ResponseEntity.ok(数据库所有者);
}
@DeleteMapping(value=“/{ownerId}/delete”)
public ResponseEntity deleteOwner(@PathVariable(value=“ownerId”)long ownerId,@PathVariable(value=“countryId”)long countryId){
OwnerId ownerKey=新的OwnerId(OwnerId,countryId);
ownerRepository.deleteById(ownerKey);
返回ResponseEntity.ok().build();
}
}  
@RestController
@请求映射(value=“/api/country/”)
公共类控制器{
@自动连线
私有国家知识库国家知识库;
@后映射(“添加”)
公共响应地址国家(@RequestBody Country){
试一试{
返回ResponseEntity.ok(countryRepository.save(country));
}捕获(例外e){
System.err.println(e.getMessage());
返回ResponseEntity.badRequest().build();
}
}
@DeleteMapping(“/{countryId}/delete”)
public ResponseEntity deleteCountry(@PathVariable(value=“countryId”)long countryId){
试一试{
countryRepository.deleteById(countryId);
返回ResponseEntity.ok().build();
}捕获(例外e){
System.err.println(e.getMessage());
返回ResponseEntity.badRequest().build();
}
}
}
在我的测试中,我创建并添加如下实体:

@测试
公共无效测试所有者(){
国家=新国家();
country.setName(“ExampleCountry”);
ResponseEntity ResponseEntity=restemplate.postForEntity(URL+port+“/api/country/add”、country、country.class);
Assert.assertTrue(responseEntity.getStatusCode().is2xxSuccessful());
countryId=responseEntity.getBody().getId();
所有者=新所有者();
主人
    @Entity
    @Table(name = "dog")
    @IdClass(DogId.class)
    public class Dog {

        @TableGenerator(
                name="dogGen",
                allocationSize=1)
        @Id
        @GeneratedValue(strategy = GenerationType.TABLE, generator = "dogGen")
        private long id;
    ...  
    }


    @Entity
    @Table(name = "owner")
    @IdClass(OwnerId.class)
    public class Owner {


        @TableGenerator(
                name="ownerGen",
                allocationSize=1)
        @Id
        @GeneratedValue(strategy = GenerationType.TABLE,generator = "ownerGen")
        @Column(name = "id")
        private long id;
    ...
    }