Hibernate 3.2.5
MySQL 5.0
I've been trying to map a @ManyToOne to a composite foreign key. One of the keys is in a @MappedSuperClass and is also a @ManyToOne foreign key.
Is there a way to map a composite foreign key where one of the elements is another foreign key?
The only way I got it to work is to bring the foreign key property into the entity too with insertable = false, and updatable = false.
The following works, but I would prefer to get rid of the createdById field and only have the createdBy field.
Is this possible? Any ideas?
Thanks!
Code:
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract public class BasicPersistentObject implements Creatable, Updatable,
UpdatedBy, CreatedBy, Serializable {
@ManyToOne(fetch = FetchType.LAZY)
private User updatedBy;
@ManyToOne(fetch = FetchType.LAZY)
private User createdBy;
@Column(name = "createdBy_id", insertable = false, updatable = false)
private String createdById;
@NotNull
private Date createdDate;
@NotNull
private Date updatedDate;
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Date getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(Date updatedDate) {
this.updatedDate = updatedDate;
}
public boolean isNew() {
return createdDate == null;
}
public User getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(User updatedBy) {
this.updatedBy = updatedBy;
}
public User getCreatedBy() {
return createdBy;
}
public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
this.createdById = createdBy.getId();
}
public String getCreatedById() {
return createdById;
}
public void setCreatedById(String createdById) {
this.createdById = createdById;
}
}
@MappedSuperclass
abstract public class PersistentObject extends BasicPersistentObject {
@Id
@Length(min = 36, max = 36)
@Column(columnDefinition = "CHAR (36)")
private String id = UUID.randomUUID().toString();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || !(o instanceof PersistentObject)) {
return false;
}
PersistentObject other = (PersistentObject) o;
// if the id is missing, return false
if (id == null)
return false;
// equivalence by id
return id.equals(other.getId());
}
public int hashCode() {
if (id != null) {
return id.hashCode();
} else {
return super.hashCode();
}
}
public String toString() {
return this.getClass().getName() + "[id=" + id + "]";
}
}
@Entity
public class Comment extends PersistentObject {
/**
* The parent Id that owns this thread of comments.
*/
@NotNull
@Column(columnDefinition = "CHAR (36)")
private String parentId;
@Column(length = 2000)
@NotNull
private String comment;
@ManyToOne
private Comment parentComment;
@ManyToOne
@JoinColumns( {
@JoinColumn(name = "parentId", referencedColumnName = "parentId", updatable = false, insertable = false),
@JoinColumn(name = "createdBy_id", referencedColumnName = "createdBy_id", updatable = false, insertable = false) })
private Review review;
@Length(max = 24)
private String ipAddress;
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public Comment getParentComment() {
return parentComment;
}
public void setParentComment(Comment parentComment) {
this.parentComment = parentComment;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Review getReview() {
return review;
}
public void setReview(Review review) {
this.review = review;
}
}
@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "parentId",
"createdBy_id" }) })
public class Review extends PersistentObject {
@NotNull
@Length(min = 36, max = 36)
@Column(columnDefinition = "CHAR (36)")
private String parentId;
@NotNull
private Integer rating1 = 0;
@NotNull
private Integer rating2 = 0;
@NotNull
private Integer rating3 = 0;
@NotNull
private Integer rating4 = 0;
@NotNull
private Integer rating5 = 0;
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public Integer getRating1() {
return rating1;
}
public void setRating1(Integer rating1) {
this.rating1 = rating1;
}
public Integer getRating2() {
return rating2;
}
public void setRating2(Integer rating2) {
this.rating2 = rating2;
}
public Integer getRating3() {
return rating3;
}
public void setRating3(Integer rating3) {
this.rating3 = rating3;
}
public Integer getRating4() {
return rating4;
}
public void setRating4(Integer rating4) {
this.rating4 = rating4;
}
public Integer getRating5() {
return rating5;
}
public void setRating5(Integer rating5) {
this.rating5 = rating5;
}
}
[/code]