@Entity public class Master {
@Id
@Column(name = "SIGNIFICANT_KEY")
private String realKey;
@Id
@Column(name = "CONSTANT_KEY")
private String constantPartKey;
}
I have a detailed information Table B, using only one (non-constant) column to refer to the main table A. I want to achieve the usual ManyToOne and OneToMany relationship between the two tables.
Question: How to use Hibernate to handle this situation?
The only master reference solution I found relies on using the formula:
@Entity public class Detail {
@ManyToOne
@JoinColumnsOrFormulas(value={
@JoinColumnOrFormula(column=
@JoinColumn(name = "SIGNIFICANT_KEY",
referencedColumnName = "SIGNIFICANT_KEY",
insertable=false, updatable=false, nullable = false)),
@JoinColumnOrFormula(formula=
@JoinFormula(referencedColumnName="CONSTANT_KEY", value="'THE CONSTANT VALUE'"))
})
Master master;
}
Now I see other problems: due to the java.lang.ClassCastException I reported earlier, I cannot use this field in the OneToMany relationship: https://hibernate.onjira .com/browse/HHH-6811
What I want to do is to map only non-constant columns to IDs, and make constant columns a regular column with constant values:
@Entity
public class Strange {
@Id
private Long id;
@Column
private long constant = 345; // the constant that you want in yo ur database
@OneToMany(mappedBy = "strange")
private Setdetails;
...
// no setter for constant!
}
@Entity
public class Detail {
...
@ManyToOne
@JoinColumn(name = "strange_id" )
private Strange strange;
}
If other entities use its full composite key to refer to the same Strange entity, you only need to do the following:
< /p>
@Entity
public class Strange {
@Id
@Column(name = "strange_id")
private Long id;
@Id
private long constant = 345; // the constant that you want in your database
@OneToMany(mappedBy = "strange")
private Setdetails ;
...
// no setter for constant!
}
@Entity
public class Detail {
...
@ManyToOne
@JoinColumn(name = "strange_id", referencedColumnName = "strange_id")
private Strange strange;
}
You can Use other entities other than PK to refer to other entities. Another unique column will also work. And because of the strange _id is unique and it meets the requirements. In this case, the referenced column name must be specified using the referencedColumnName attribute.
I have a main table A, which contains a The composite primary key consists of two columns. One of the columns is a constant (“constant value” in the code below). The table definition is as follows:
@Entity public class Master {
@Id
@Column(name = "SIGNIFICANT_KEY")
private String realKey;
@Id
@Column(name = "CONSTANT_KEY")
private String constantPartKey;
}
I have a detailed information table B that uses only one (non-constant) column to reference main table A. I want to use two Realize the usual ManyToOne and OneToMany relationships between tables.
Question: How to use Hibernate to handle this situation?
The only master reference solution I found relies on using the formula:
@Entity public class Detail {
@ManyToOne
@JoinColumnsOrFormulas(value={
@JoinColumnOrFormula(column=
@JoinColumn(name = "SIGNIFICANT_KEY",
referencedColumnName = "SIGNIFICANT_KEY",
insertable=false, updatable=false, nullable = false)),
@JoinColumnOrFormula(formula=
@JoinFormula(referencedColumnName="CONSTANT_KEY", value="'THE CONSTANT VALUE'"))
})
Master master;
}
Now I see other problems: due to the java.lang.ClassCastException I reported earlier, I cannot use this field in the OneToMany relationship: https://hibernate.onjira .com/browse/HHH-6811
I really want to know how useful it is to have a constant column value in the database, but anyway… < p>
What I want to do is only map the non-constant column to ID, and make the constant column a regular column with constant value:
@ Entity
public class Strange {
@Id
private Long id;
@Column
private long constant = 345; // the constant that you want in your database
@OneToMany(mappedBy = "strange" )
private Setdetails;
...
// no setter for constant!
}
@Entity< br />public class Detail {
...
@ManyToOne
@JoinColumn(name = "strange_id")
private Strange strange;
}
If other entities refer to the same Strange entity using its complete composite key, you only need to do the following:
@Entity
public class Strange {
@Id
@Column(name = "strange_id")
private Long id;
@Id
private long constant = 345; // the constant that you want in your database
@OneToMany(mappedBy = "strange")
private Setdetails;
...
// no setter for constant!
}
@Entity
public class Detail {
...
@ManyToOne
@JoinColumn(name = "strange_id", referencedColumnName = "strange_id")
private Strange strange;
}
You can use entities other than PK to refer to other entities. Another unique column will also work. And because strange_id is unique, it meets the requirements. In this case, the referenced column name must be specified using the referencedColumnName property.
< /p>