0

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.

2
  • What database are you using? Commented Feb 15, 2020 at 20:07
  • I'm using PostgreSQL Commented Feb 15, 2020 at 20:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Indeed, it didn't work, the numeration got crazier =(
How did you define the sequences?
@PtF also try adding @JoinColumn to the child entity, just like here: vladmihalcea.com/…
I tried adding @JoinColumn but the problem persists =(
As mentioned you seem to be using the same sequence for both tables. Now from the DBMS perspective there is no problem with this, you might superimpose a problem, but the date base is fine with it. I've actually been involved where separate schema were created for separate subject areas. And only 1 sequence per schema. Worked just fine even when the schema contained scores of tables.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.