Problem in entity creation with one-to-one relationship

6

I created a test project, and I'm studying entity creation with relationships, and in that I created two entities with this relationship below:

Iamabletocreatetheentity,butnotintherightway,usingthisclassbelow:

packagebr.com.softplan.jpa;importjavax.persistence.EntityManager;importjavax.persistence.EntityTransaction;importbr.com.softplan.model.Pedido;publicclassCadastraCategoriaJPA{publicstaticvoidmain(String[]args){Pedidop=newPedido();p.setNome_pedido("descrocao");

        EntityManager em = PersistenceUtil.getEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            em.persist(p);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        }
        PersistenceUtil.close(em);
        PersistenceUtil.close();
    }    
}

I want to know how to create the entity with the relationships by putting the values of the attributes of the two tables.

"Order" table:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome_pedido;
@OneToOne
@JoinColumn(name = "endereco_id")
private Endereco enderecoEntrega;

Address table:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long endereco_id;
private String lougradouro;
@OneToOne(mappedBy = "enderecoEntrega")
private Pedido pedido;

This was an attempt but it did not work:

Pedido p = new Pedido();
Endereco end = new Endereco();

p.setNome_pedido("descrocao");

end.setLougradouro("hdsbcjhbsdjc");
end.setPedido(p);
    
asked by anonymous 11.09.2015 / 18:40

1 answer

1
Pedido p = new Pedido();
Endereco end = new Endereco();
end.setLogradouro("teste");
//set aqui o objeto completo do endereço
p.setEndereco(end);
    
14.09.2015 / 15:43