简体   繁体   中英

Problem with primary keys Java Hibernate?

I'm using Hibernate and Java to implement a database.

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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