Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.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 复合主键未映射 桌子: 实体类: @Setter @吸气剂 @实体 @表(name=“父表”) 公共类父级实现可序列化{ 私有静态最终长serialVersionUID=1L; @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) @列(name=“parent\u id”) 私有int-parentId; @OneToMany(mappedBy=“parent”,cascade=CascadeType.ALL,orphan=true) private List childs=new ArrayList(); } @吸气剂 @塞特 @EqualsAndHashCode 公共类ChildPK实现可序列化{ //可序列化类所需的默认串行版本id。 私有静态最终长serialVersionUID=1L; @列(name=“parent\u id”,null=false) 私有int-parentId; @列(name=“child\u id”) 私人智力儿童; } @吸气剂 @塞特 @EqualsAndHashCode @实体 @IdClass(ChildPK.class) @表(name=“child\u Table”) 公共类子级实现可序列化{ 私有静态最终长serialVersionUID=1L; @身份证 @列(name=“child\u id”) 私人智力儿童; @身份证 @TableGenerator(name=“child\u id\u generator”,table=“id\u sequence”,initialValue=1,allocationSize=1) @GeneratedValue(策略=GenerationType.TABLE,generator=“子项id\U生成器”) @manytone(fetch=FetchType.LAZY) @JoinColumn(name=“parent\u id”,insertable=false,updateable=false) 私人家长; } 类主类{ 公共静态void main(字符串[]args) { entityManager.persist(parent);//在子表中父id存储为0 } }_Java_Mysql_Hibernate_Jpa - Fatal编程技术网

Java 复合主键未映射 桌子: 实体类: @Setter @吸气剂 @实体 @表(name=“父表”) 公共类父级实现可序列化{ 私有静态最终长serialVersionUID=1L; @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) @列(name=“parent\u id”) 私有int-parentId; @OneToMany(mappedBy=“parent”,cascade=CascadeType.ALL,orphan=true) private List childs=new ArrayList(); } @吸气剂 @塞特 @EqualsAndHashCode 公共类ChildPK实现可序列化{ //可序列化类所需的默认串行版本id。 私有静态最终长serialVersionUID=1L; @列(name=“parent\u id”,null=false) 私有int-parentId; @列(name=“child\u id”) 私人智力儿童; } @吸气剂 @塞特 @EqualsAndHashCode @实体 @IdClass(ChildPK.class) @表(name=“child\u Table”) 公共类子级实现可序列化{ 私有静态最终长serialVersionUID=1L; @身份证 @列(name=“child\u id”) 私人智力儿童; @身份证 @TableGenerator(name=“child\u id\u generator”,table=“id\u sequence”,initialValue=1,allocationSize=1) @GeneratedValue(策略=GenerationType.TABLE,generator=“子项id\U生成器”) @manytone(fetch=FetchType.LAZY) @JoinColumn(name=“parent\u id”,insertable=false,updateable=false) 私人家长; } 类主类{ 公共静态void main(字符串[]args) { entityManager.persist(parent);//在子表中父id存储为0 } }

Java 复合主键未映射 桌子: 实体类: @Setter @吸气剂 @实体 @表(name=“父表”) 公共类父级实现可序列化{ 私有静态最终长serialVersionUID=1L; @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) @列(name=“parent\u id”) 私有int-parentId; @OneToMany(mappedBy=“parent”,cascade=CascadeType.ALL,orphan=true) private List childs=new ArrayList(); } @吸气剂 @塞特 @EqualsAndHashCode 公共类ChildPK实现可序列化{ //可序列化类所需的默认串行版本id。 私有静态最终长serialVersionUID=1L; @列(name=“parent\u id”,null=false) 私有int-parentId; @列(name=“child\u id”) 私人智力儿童; } @吸气剂 @塞特 @EqualsAndHashCode @实体 @IdClass(ChildPK.class) @表(name=“child\u Table”) 公共类子级实现可序列化{ 私有静态最终长serialVersionUID=1L; @身份证 @列(name=“child\u id”) 私人智力儿童; @身份证 @TableGenerator(name=“child\u id\u generator”,table=“id\u sequence”,initialValue=1,allocationSize=1) @GeneratedValue(策略=GenerationType.TABLE,generator=“子项id\U生成器”) @manytone(fetch=FetchType.LAZY) @JoinColumn(name=“parent\u id”,insertable=false,updateable=false) 私人家长; } 类主类{ 公共静态void main(字符串[]args) { entityManager.persist(parent);//在子表中父id存储为0 } },java,mysql,hibernate,jpa,Java,Mysql,Hibernate,Jpa,在子表中,父id列作为外键和复合主键的一部分 嵌入类内部无法使用标识生成器。所以我在这里使用ID类。为子_id列生成自动增量值 我无法将从父\u表生成的父\u id值作为外键值存储到子\u表中,它将存储为0 有人能帮我检查一下映射吗 提前感谢…如果子id已经是唯一的,为什么需要父id作为主键的一部分?只需使用: @Setter @Getter @Entity @Table(name = "parent_table") public class Parent implement

在子表中,父id列作为外键和复合主键的一部分

嵌入类内部无法使用标识生成器。所以我在这里使用ID类。为子_id列生成自动增量值

我无法将从父\u表生成的父\u id值作为外键值存储到子\u表中,它将存储为0

有人能帮我检查一下映射吗


提前感谢…

如果子id已经是唯一的,为什么需要父id作为主键的一部分?只需使用:

@Setter
@Getter
@Entity
@Table(name = "parent_table")
public class Parent implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "parent_id")
    private int parentId;
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
    private List<Child> childs=new ArrayList<Child>();
    
    }


@Getter
@Setter
@EqualsAndHashCode
@Entity
@Table(name = "child_table")
public class Child implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="child_id")
    @TableGenerator(name = "child_id_generator", table = "id_sequence", initialValue = 1, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "child_id_generator")
    private int childId;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private Parent parent;
    
    }
@Setter
@吸气剂
@实体
@表(name=“父表”)
公共类父级实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“parent\u id”)
私有int-parentId;
@OneToMany(mappedBy=“parent”,cascade=CascadeType.ALL,orphan=true)
private List childs=new ArrayList();
}
@吸气剂
@塞特
@EqualsAndHashCode
@实体
@表(name=“child\u Table”)
公共类子级实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@列(name=“child\u id”)
@TableGenerator(name=“child\u id\u generator”,table=“id\u sequence”,initialValue=1,allocationSize=1)
@GeneratedValue(策略=GenerationType.TABLE,generator=“子项id\U生成器”)
私人智力儿童;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“parent\u id”)
私人家长;
}
@Setter
@吸气剂
@实体
@表(name=“父表”)
公共类父级实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“parent\u id”)
私有int-parentId;
@OneToMany(mappedBy=“parent”,cascade=CascadeType.ALL,orphan=true)
private List childs=new ArrayList();
}
@吸气剂
@塞特
@EqualsAndHashCode
@可嵌入
公共类ChildPK实现可序列化{
//可序列化类所需的默认串行版本id。
私有静态最终长serialVersionUID=1L;
@列(name=“parent\u id”,null=false)
私有int-parentId;
@TableGenerator(name=“child\u id\u generator”,table=“id\u sequence”,initialValue=1,allocationSize=1)
@GeneratedValue(策略=GenerationType.TABLE,generator=“子项id\U生成器”)
@列(name=“child\u id”)
私人智力儿童;
}
@吸气剂
@塞特
@EqualsAndHashCode
@实体
@表(name=“child\u Table”)
公共类子级实现可序列化{
私有静态最终长serialVersionUID=1L;
@嵌入的
private ChildPK ChildPK=new ChildPK();
@manytone(fetch=FetchType.LAZY)
@MapsId(“父ID”)
@JoinColumn(name=“parent\u id”,insertable=false,updateable=false)
私人家长;
}
类主类{
公共静态void main(字符串[]args)
{
entityManager.persist(parent);//在子表中父id存储为0
}
}

根据业务规则,它是复合主键。谢谢@Christian Beikov。
@Setter
@Getter
@Entity
@Table(name = "parent_table")
public class Parent implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "parent_id")
    private int parentId;
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
    private List<Child> childs=new ArrayList<Child>();
    
    }


@Getter
@Setter
@EqualsAndHashCode
public class ChildPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="parent_id",nullable = false) 
    private int parentId;

    @Column(name="child_id")
    private int childId;
}



@Getter
@Setter
@EqualsAndHashCode
@Entity
@IdClass(ChildPK.class)

@Table(name = "child_table")
public class Child implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="child_id")
    private int childId;
     
      
    @Id
    @TableGenerator(name = "child_id_generator", table = "id_sequence", initialValue = 1, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "child_id_generator")
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id",insertable = false,updatable = false)
    private Parent parent;
    
    }

class MainClass{
    
    public static void main(String[] args)
    {
    entityManager.persist(parent);//in child_table parent_id storing as 0
    }
    }
@Setter
@Getter
@Entity
@Table(name = "parent_table")
public class Parent implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "parent_id")
    private int parentId;
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
    private List<Child> childs=new ArrayList<Child>();
    
    }


@Getter
@Setter
@EqualsAndHashCode
@Entity
@Table(name = "child_table")
public class Child implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="child_id")
    @TableGenerator(name = "child_id_generator", table = "id_sequence", initialValue = 1, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "child_id_generator")
    private int childId;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private Parent parent;
    
    }
@Setter
@Getter
@Entity
@Table(name = "parent_table")
public class Parent implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "parent_id")
    private int parentId;
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
    private List<Child> childs=new ArrayList<Child>();
    
    }


@Getter
@Setter
@EqualsAndHashCode
@Embeddable
public class ChildPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="parent_id",nullable = false) 
    private int parentId;

        @TableGenerator(name = "child_id_generator", table = "id_sequence", initialValue = 1, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "child_id_generator")
    @Column(name="child_id")
    private int childId;
}

@Getter
@Setter
@EqualsAndHashCode
@Entity
 @Table(name = "child_table")
    public class Child implements Serializable {
        private static final long serialVersionUID = 1L;

        @EmbeddableId
        private ChildPK childPk=new ChildPk(); 
   
        @ManyToOne(fetch = FetchType.LAZY)
        @MapsId("parentId")
        @JoinColumn(name = "parent_id",insertable = false,updatable = false)
        private Parent parent;
        
        }

class MainClass{
    
    public static void main(String[] args)
    {
    entityManager.persist(parent);//in child_table parent_id storing as 0
    }
    }