I have a couple scenarios, one typical the other not so much, that I am
trying to implement as quickly and easily as possible.
The first is pretty straight-forward:
@Entity
public class Application
...
private StreetSection streetSecion;
...
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name="streetSection", nullable=false)
public StreetSection getStreetSection()
{
return streetSection;
}
In my form, which is the correct way to do the input?
<form:hidden path="streetSection" id="streetSection" />
or
<form:hidden path="streetSection.id" id="streetSection.id" />
In my testing I have found that they both work but each one has its own
little quirks. For example, if I use 'streetSection.id', when it's a new
Application the page crashes because streetSection.id is null. So I have to
put a <c:if> in there and if it's null just use a normal <input
type='hidden'> tag.
If I use 'streetSection' I believe (I've done so many tests I forget ...)
that Hibernate is giving me 'can't save transient object' errors. Anyway,
just curious what the best practice is.
The other scenario is more complicated. I have an object, Establishment, and
another, Container, and they both have a list of EstablishmentContainer
objects, which saved in a join table that has an attribute, quantity. So
each POJO, Establishment and Container, have a OneToMany relationship with
EstablishmentContainer, and EstablishmentContainer has a ManyToOne
relationship with both Establishment and Container. This is apparently the
best way to map join tables with attributes.
@Entity
public class Establishment
...
private List<EstablishmentContainers> establishmentContainers;
...
@OneToMany(mappedBy="establishment", fetch = FetchType.EAGER, cascade = {
CascadeType.PERSIST, CascadeType.MERGE })
public List<EstablishmentContainer> getEstablishmentContainers()
{
return establishmentContainer;
}
@Entity
public class Container
...
@OneToMany(mappedBy="container")
public List<EstablishmentContainer> getEstablishmentContainers ()
{
return establishmentContainers;
}
@Entity
public class EstablishmentContainer extends
{
private Long id;
private Establishment establishment;
private Container container;
private int quantity;
...
@ManyToOne
@JoinColumn(name="idEstablishment")
public Establishment getEstablishment()
{
return establishment;
}
@ManyToOne
@JoinColumn(name="idContainer")
public Cubo getContainer()
{
return container;
}
@Column(name="quantity")
public int getQuantity()
{
return quantity;
}
In my EstablishmentFormController do I have to do the following for each
EstablishmentContainer created in the page:
1. instantiate an EstablishmentContainer object
2. fill it it request parameters by hand
3. add it to a List
4. assign that list to my Establishment object
... for the EstablishmentContainers list to be properly saved when I do an
establishmentManager.save()?
This is what I am doing currently and it works but it seems to me that if I
put the following in my jsp:
input type="text" id="establishmentContainers.container.id"
input type="text" id="establishmentContainers.establihsment.id"
input type="text" id="establishmentContainers.quantity"
Spring should be able to populate the List of EstablishmentObjects
automatically, just as in the first example it populates my
Application.StreetSection object when I put a
input type="text" id="application.streetSection"
element in the page. If it's not possible I can keep doing it the way I
currently do, I just don't want to reinvent the wheel.
Sorry for the long post. I look forward to any and all responses.
Bob
--
View this message in context:
http://www.nabble.com/Best-way-to-save-ManyToOne-relationships-with-Hibernate-and-Spring-MVC-tf4553758s2369.html#a12995187
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]