Skip to main content

how to persist foreign key only if entry doesn't exist?

1 reply [Last post]
rrlangly
Offline
Joined: 2012-06-25

The Car table is a list of cars, all unique.
The Engine table is a list of engines, all unique.
The Report table has a reference to the Car table as shown below. There can be many reports, but I don't want to duplicate anything in the Car or Engine tables.

My problem is I want to insert a Report, but only insert a car into the Car table if its not already in the Car table. Currently, all this works with the exception that Cars are being duplicated into the Car table when I add a report.

Any help much appreciated.

<br />
	ireport = (IReportBean) (new InitialContext()).lookup("java:comp/env/ejb/ReportBean");</p>
<p>	Car car = new Car(c);<br />
	Report report = new Report(r);<br />
	report.addCar(car);</p>
<p>	ireport.addReport(report);<br />

And my EntityManager basically does ...

<br />
	em.persist(report);<br />
	em.flush();<br />

My table structure ...

<br />
CREATE SEQUENCE report_seq START 1000;<br />
CREATE TABLE Report (<br />
    id          INTEGER DEFAULT NEXTVAL('report_seq') PRIMARY KEY,<br />
    car_id      INTEGER REFERENCES Car (id),<br />
    condition   VARCHAR(32) NOT NULL<br />
);</p>
<p>CREATE SEQUENCE car_seq START 1000;<br />
CREATE TABLE Car (<br />
    id          INTEGER DEFAULT NEXTVAL('car_seq') PRIMARY KEY,<br />
    engine_id   INTEGER REFERENCES Engine (id),<br />
    mfg         VARCHAR(32) NOT NULL<br />
);</p>
<p>CREATE SEQUENCE engine_seq START 1000;<br />
CREATE TABLE Engine (<br />
    id          INTEGER DEFAULT NEXTVAL('engine_seq') PRIMARY KEY,<br />
    year        VARCHAR(32) NOT NULL,<br />
    description VARCHAR(128) DEFAULT NULL<br />
);<br />

Annotations used in my Report entity.

<br />
@SequenceGenerator(name="reportIdGen", initialValue=1000, allocationSize=50,<br />
    sequenceName="report_seq")<br />
public class Report implements Serializable {<br />
    @Id<br />
    @Column(name="id")<br />
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="reportIdGen")<br />
    private Integer id;</p>
<p>    @ManyToOne(cascade=CascadeType.PERSIST)<br />
    @JoinColumn(name="car_id")<br />
    private Car carId;</p>
<p>    @Column(name="condition")<br />
    private String condition;</p>
<p>	// ...<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cetina
Offline
Joined: 2008-01-30

Why you don't find the car and if the car doesn't exists persist it
El 26/09/2012 19:50, escribió:

> The Car table is a list of cars, all unique. The Engine table is a list of
> engines, all unique. The Report table has a reference to the Car table as
> shown below. There can be many reports, but I don't want to duplicate
> anything in the Car or Engine tables. My problem is I want to insert a
> Report, but only insert a car into the Car table if its not already in the
> Car table. Currently, all this works with the exception that Cars are being
> duplicated into the Car table when I add a report. Any help much
> appreciated.
>

 ireport = (IReportBean) (new<br />
> InitialContext()).lookup("**java:comp/env/ejb/ReportBean")**; Car car =<br />
> new<br />
> Car(c); Report report = new Report(r); report.addCar(car);<br />
> ireport.addReport(report); 
And my EntityManager basically does ...
> em.persist(report); em.flush(); My table structure ...
>
<br />
> CREATE SEQUENCE report_seq START 1000; CREATE TABLE Report ( id INTEGER<br />
> DEFAULT NEXTVAL('report_seq') PRIMARY KEY, car_id INTEGER REFERENCES Car<br />
> (id), condition VARCHAR(32) NOT NULL ); CREATE SEQUENCE car_seq START 1000;<br />
> CREATE TABLE Car ( id INTEGER DEFAULT NEXTVAL('car_seq') PRIMARY KEY,<br />
> engine_id INTEGER REFERENCES Engine (id), mfg VARCHAR(32) NOT NULL );<br />
> CREATE<br />
> SEQUENCE engine_seq START 1000; CREATE TABLE Engine ( id INTEGER DEFAULT<br />
> NEXTVAL('engine_seq') PRIMARY KEY, year VARCHAR(32) NOT NULL, description<br />
> VARCHAR(128) DEFAULT NULL ); 
Annotations used in my Report entity.
>
 @SequenceGenerator(name="**reportIdGen", initialValue=1000,<br />
> allocationSize=50, sequenceName="report_seq") public class Report<br />
> implements<br />
> Serializable { @Id @Column(name="id")<br />
> @GeneratedValue(strategy=**GenerationType.SEQUENCE,<br />
> generator="reportIdGen")<br />
> private Integer id; @ManyToOne(cascade=**CascadeType.PERSIST)<br />
> @JoinColumn(name="car_id") private Car carId; @Column(name="condition")<br />
> private String condition; // ... 

>
> --
>
> [Message sent by forum member 'rrlangly']
>
> View Post: http://forums.java.net/node/**890713
>
>
>