简体   繁体   English

Java Hibernate 的主键问题?

[英]Problem with primary keys Java Hibernate?

I'm using Hibernate and Java to implement a database.我正在使用 Hibernate 和 Java 来实现数据库。

I have created the following two annotations:我创建了以下两个注释:

    @Entity public class Cargo implements Serializable{
    @Id 
    @GeneratedValue 
    private Long id;
    private String cargo;

    @OneToOne(mappedBy="cargo")
    private Funcionario funcionario;
}

and

@Entity public class Funcionario implements Serializable{

@Id @GeneratedValue private Long id;
private String nome;
private String sexo;
private String telefone;
@OneToOne
private Cargo cargo;
}

Then I try to insert data with the following code:然后我尝试使用以下代码插入数据:

 EntityManagerFactory factory = Persistence.createEntityManagerFactory("BD04PU");
        EntityManager manager = factory.createEntityManager();
        manager.getTransaction().begin();


        Cargo novoCargo = new Cargo();
        Funcionario novoFuncionario = new Funcionario();

        Scanner entrada = new Scanner(System.in); 

        System.out.print("\nCargo: "); 

        novoCargo.setCargo(entrada.nextLine());
        manager.persist(novoCargo);



        System.out.print("\nNome: ");
        novoFuncionario.setNome(entrada.nextLine());

        System.out.print("\nSexo: ");
        novoFuncionario.setSexo(entrada.nextLine());

        System.out.print("\nTelefone: ");
        novoFuncionario.setTelefone(entrada.nextLine());

        novoFuncionario.setCargo(novoCargo);


        manager.persist(novoFuncionario);
        manager.getTransaction().commit();
        factory.close();


but as the result the foreign keys of Cargo and Funcionario count as follows: Cargo starts with 1 whereas Funcionario starts with 2, then Cargo primary key becomes 3 and Funcionario primary key becomes 4.但结果 Cargo 和 Funcionario 的外键计数如下:Cargo 从 1 开始,而 Funcionario 从 2 开始,然后 Cargo 主键变为 3,Funcionario 主键变为 4。

It is as if both primary keys were the same.就好像两个主键是相同的。

Why is that so?为什么呢? How to fix it?如何解决?

Above, the Cargo id should be used as a foreign key in the Funcionario table.上面,货物 id 应该用作 Funcionario 表中的外键。

You're reusing the same sequence in both entities, to separate them, you can use您在两个实体中重复使用相同的序列,要将它们分开,您可以使用

@Entity 
public class Cargo implements Serializable{
    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cargo_generator")
    @SequenceGenerator(name="cargo_generator", sequenceName = "cargo_seq", allocationSize=50)
    private Long id;
    private String cargo;

    @OneToOne(mappedBy="cargo")
    private Funcionario funcionario;
}

and

@Entity 
public class Funcionario implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "funcionario_generator")
    @SequenceGenerator(name="funcionario_generator", sequenceName = "funcionario_seq", allocationSize=50)
    private Long id;
    private String nome;
    private String sexo;
    private String telefone;
    @OneToOne
    private Cargo cargo;

}

If you're creating the schema manually, you would have to define the sequences: cargo_seq and funcionario_seq .如果您手动创建架构,则必须定义序列: cargo_seqfuncionario_seq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM