質問

In the Spring/Hibernate/Java/Tomcat app I'm writing I have a OneToMany relationship between an Organization and its Contacts.

Organization 1:M Contact (has foreign key org_id)

In Organization I have this field:

@OneToMany(mappedBy="organization")
private List<Contact> contacts;

In Contact I have this field:

@ManyToOne
@JoinColumn(name="org_id")
private Organization organization;

All is working OK so far. Now I'm adding the concept of an Offer. The Offer can be made by an Organization, and you speak with the designated Contact for that particular Offer.

Offer has foreign keys for its organization (org_id) and designated contact (contact_id).

So far, the Offer would look like:

@OneToOne
@JoinColumn(...)
private Organization offering_org;

@OneToOne
@JoinColumn(...)
private Contact offering_contact;

Here comes the point of my question. I've already annotated the Contact class for use with Organization. If I try to persist the Offer object in the usual Hibernate way, I'll need to store copies of an Organization object and a Contact object into the Offer object. This seems to conflict with my existing Organization : Contact use of the two Java classes. For example, if I've a 1:1 with Offer, if I put this into the Contact class do I get an optional use of either or a mandatory simultaneous use of both?

Since the Offer is yet another relationship, do I need to write a data transfer object version of Contact for use in the Offer relationship?

Thanks, Jerome.

役に立ちましたか?

解決 2

I'm thinking that my original question is kind of stupid. What I did try is to put this in Offer.java:

@Column(name="org_id")
private Long orgId = null;

@Column(name="contact_id")
private Long contactId = null;

I fill orgId manually because an offer is always tied to the user's Organization. It is a hidden field in the web page.

I put a SELECT filled with appropriate Contact objects (contact.id, contact.name) in the web page.

When the web page is submitted the Offer's orgId and contactId fields are filled in the @ModelAttribute parameter. This takes me where I want to go.

To address the comments of Mr. mspringer, your example could work (you illustrated a "create new" situation) if I were willing to use an Organization or Contact list in my Offer object. It is also somewhat the topic of my original question. But since I see that I don't really want to play with the expanded objects within Offer, nor do I wish to, I can avoid the topic of my original question.

Thanks to all who looked at my exercise in confusion.

他のヒント

Perhaps I do not fully understand the problem but I'd just do something like this:

// contact & organization being already persisted entity objects
Offer offer = new Offer();
offer.setOffering_org(organization);
offer.setOffering_contact(contact);

// Persisting the new Offer object to the database, 
// implicitly making the relations.
service.saveObject(offer);

I see no reason to create copy(s) of the organization object?
It just happens to be that the collection of "contacts" in the Organization object can also be a Contact within one or more Offer objects.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top