1

I have three relationships that are causing me issues.

<code> 
@Entity
@Table(name="area")
public class Area implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name="name")
private String name;
...................   

}

@Entity
@Table(name="law_connection")
public class Connection implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;


@OneToOne(fetch=FetchType.LAZY,cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name="client")
private Client client;

@OneToMany(fetch=FetchType.LAZY,targetEntity=Area.class, cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name="areas")
private List<Area> areas;

……………………    
}



@Entity
@Table(name=“worker”)

public class Worker implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;


@Column(name="firstName")        
private String firstName;

@Column(name="lastName")
private String lastName;

@OneToOne(fetch=FetchType.LAZY,cascade = {CascadeType.MERGE, CascadeType.REFRESH,    CascadeType.DETACH})
@JoinColumn(name="organisation")
private Organisation organisation;     


@Column(name="status")
@Enumerated(EnumType.ORDINAL)
private WorkerState state;

@OneToMany(fetch=FetchType.LAZY,targetEntity=Area.class,cascade = {CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name="areas")
private List<Area> areas;

………………..    
}

</code>

What happens in the database is this:

<code>
mysql> select * from  area;
+------+-----------------------+------------+
| ID   | name                  | areas |
+------+-----------------------+------------+
| 1601 | Unknown               |       NULL |
| 1602 | Someplace                   |       NULL |
| 1603 | Someotherplace                 |       NULL |
| 1604 | Someplace2      |       NULL |
| 1605 | Someplace3          |       NULL |
| 1606 | Someplace4               |       NULL |
| 1607 | Someplace5 |       NULL |
| 1608 | Someplace6             |       NULL |
| 1609 | Someplace7         |       NULL |
| 1610 | Someplace7          |       1652 |
| 1611 | Someplace8              |       NULL |
+------+-----------------------+------------+
11 rows in set (0.00 sec)
</code

What observe is that there can only be one object that points to an area, What have I misunderstood here?

I needed a worker to have many areas and a connection to have at least one what am I doing wrong

Damien Cooke
  • 599
  • 10
  • 20
  • You omit to say HOW you persisted those objects. So you only added one Area to the "areas" collection before persisting perhaps? who knows based on the info above. Also you are reusing the "areas" column in Area table for two separate purposes, which is prone to failure ... how will it know if the Area is a worker or a connection "area" ???? – Neil Stockton Dec 06 '14 at 11:05
  • Thanks Neil, please explain further if you dont mind on your second comment as it sounds like that is where the problem is. So do I need to use something like @JoinColumn(name="workers") in the worker entity and @JoinColumn(name="connections") in the connection entity? – Damien Cooke Dec 06 '14 at 11:21

1 Answers1

1

You can't share an external foreign key between two separate collections. You currently have "areas" column in Area representing whether the Area is in Connection.areas, and also representing whether the Area is in Worker.areas. If it had a value in that column how would it know which collection it relates to ? (when it tries to retrieve Worker.areas it will simply look for all Area rows that have the "areas" column set to the particular Worker "id" ... similarly for Connection.areas).

Better option is to have separate FK columns in Area, one called WORKER_ID, and the other called CONNECTION_ID for example. That way an Area can be in Worker.areas, and also in Connection.areas and the relations are handled separately.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29