2

I have a problem with a relation on Hibernate:

I have two Entities --> Solicitud and DetalleAccesorio, and the relation is:

I have one Solicitud with Many DetalleAccesorio, and I need to save the Solicitud with the DetalleAccesorio, and both are diferents tables on the database.. this is the important code of Solicitud:

@Id @GeneratedValue( strategy=GenerationType.IDENTITY ) @Column( name="num_solicitud" ) private Long numSolicitud;

 @OneToMany( fetch=FetchType.LAZY, mappedBy="codDetalle", cascade={ CascadeType.ALL } )
 private List<DetalleAccesorio> listaAccesorios;     `

What I need to save the same primary key of Solicitud on DetalleAccesorio?

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
roaro
  • 21
  • 3
  • Are you asking what is needed to save the primary key for your Solicitud table? Also, what error are you receiving? – Woot4Moo Jul 31 '12 at 20:38
  • set the primary key of DetalleAccesorio with the primary key of Solicitud and save it. – roaro Jul 31 '12 at 20:43
  • Ok and what is happening? Is an exception being thrown or is it not persisting? – Woot4Moo Jul 31 '12 at 20:46
  • no.. I dont know how to make the relation on the DetalleAccesorio entity with the Solicitud entity. – roaro Jul 31 '12 at 20:48

4 Answers4

1

Basically you have to set the relation in the child entity (DetalleAccesorio) like:

@ManyToOne
Solicitud codDetalle

Take a look to this thread and also this documentation

Community
  • 1
  • 1
Pablo Lascano
  • 662
  • 6
  • 14
0

Remove the mappedBy="codDetalle" from the relation. the mapped by create bidirectional relation . in bidorectional relation the side with mappedby (the onetomany) not control on the relation any more. this is the reason that hibernate dosnt recognize that there is a relation here. If you want to have bidirectional than implement it as it need to be. means both side need to annotate and the important part - you have to maintain the reference in java both ways! look on this.

as you can see there in bidirectional (and i dont think that you need , so remove the mapped by and it will be solved ) you need in code to maintain 2 directions:

Changes made only to the inverse end of the association are not persisted.

Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55
0

In fact it has a very simple solution

If you are willing to have access to Solicitud from DetalleAccesorio:
In the DetalleAccesorio class you have to have this:

@ManyToOne(fetch=fetchType.EAGER)
Solicitud codDetalle

It was optional.

(It's not optional) In the Solicitud try this:

@OneToMany(fetch=FetchType.LAZY)
@JoinTable(name = "Solicitud_DetalleAccesorio_MAPPING", joinColumns = @JoinColumn(name = "DetalleAccessorio_ID"), inverseJoinColumns = @JoinColumn(name = "Solicitu_ID"))
List<ManagerDetails> managerDetails;
Matin Kh
  • 5,192
  • 6
  • 53
  • 77
0

this is the code from the entity DetalleAccesorio

@Id
@Column( name="cod_detalle" )
private Long codDetalle;

@Column( name="cod_accesorio" )
private Integer codAccesorio;

And the id "codDetalle" has to be the same id from Solicitud to save it (numSolicitud)...

roaro
  • 21
  • 3