I'm trying to create a ManyToOne relation between two entities. The tables are looking like this:
Table:EQUIPMENT
----------------
ID (Integer PK)
FIXTURE_NO(Varchar 16)
Table:FIXTURE
----------------
ID(Integer PK)
TNO(Varchar 4)
DNO(Varchar 12)
While creating entities, I want to create a many to one relation where fixture No = tNo+dNo (concatenation of these two values.)
Here are the code samples of my models:
@Entity
@Table(name="EQUIPMENT", schema="EQP")
public class Equipment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@NotNull
@Column(unique=true)
private int id;
@NotNull
@ManyToOne
@JoinColumn(name="FIXTURE_NO")
private Fixture fixture;
//getters and setters etc...
}
@Entity
@Table(name="FIXTURE", schema="FIX")
public class Fixture {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@NotNull
@Column(unique=true)
private int id;
@Size(max=4)
@Column(name="TNO")
private String tNo;
@Size(max=12)
@Column(name="DNO")
private String dNo;
//getters and setters etc...
}
Right now, I see that the ManyToOne mapping is relating to EQUIPMENT.FIXTURE_NO with FIXTURE.ID, but I couldn't find a way to map the concatenation of TNO and DNO with FIXTURE_NO.
Thanks for the help!
It will not work with concatenation but if you change your datamodel to:
Table:EQUIPMENT
----------------
ID (Integer PK)
TNO(Varchar 4)
DNO(Varchar 12)
Table:FIXTURE
----------------
ID(Integer PK)
TNO(Varchar 4)
DNO(Varchar 12)
You can add a relationship like this:
@ManyToOne
@JoinColumns({
@JoinColumn(name="TNO", referencedColumnName="TNO"),
@JoinColumn(name="DNO", referencedColumnName="DNO")
})
private Fixture fixture;