Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
JPAContainer - many-to-one or one-to-one mapping
This question may be more of a Hibernate or Eclipse issue, but I hope someone can help me... I suspect the answer to this question may help solve the question I asked about the JPAContainer and the foreign key filtering yesterday...
I have a database with 2 tables that are related as follows (I've edited out lots of extraneous stuff). Using Eclipse, I have created java classes for the tables (new entities from tables).
public class RrdRecord implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name="rrd_path")
private String rrdPath;
//bi-directional many-to-one association to TrafficReportPort
@OneToMany(mappedBy="rrdRecord",fetch=FetchType.EAGER)
private Set<TrafficReportPort> trafficReportPorts;
etc. etc.
The fact is, that there is actually at MOST a single TrafficReportPort record for a given RRDRecord object (and there may be no record at all). So I would like to change the OneToMany mapping to a OneToOne mapping and use a single TrafficReportPort object instead of a Set. But Eclipse just tells me that OneToOne is an illegal statement in that location.
I added a routine called getTrafficReportPort which looks at the set and returns the one element if it is there. By doing that, I was able to add a nested property to one of my tables and it displays the trafficReportPort description field successfully. But where it fails is trying to construct a filter and I think that is because the property is a set instead of a single object.
So my question is - how do I describe the system so I have a one-to-one mapping instead of one-to-many? If I can fix that, perhaps my filter problem will go away, but I won't know until I get past this one...
Thanks in advance,
nbc
After much digging, I found the correct way to create a one-to-one mapping, and that seems to have solved this issue, as well as the JPAContainer filter problem that I asked about the other day.
For the record, the code looks like this:
In the RRDRecord class:
@OneToOne(mappedBy="rrdRecord",fetch=FetchType.EAGER)
private TrafficReport trafficReport;
and in the TrafficReport object:
@OneToOne
@PrimaryKeyJoinColumn
private RrdRecord rrdRecord;
This, along with the appropriate getters and setters seems to work.
nbc