I'm mapping a Filter ---< FilterColumn where Filter presents cardinality one and FilterColumn N. So the mapped classes are:
@Entity
public class Filter implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String name;
private String caption;
@OneToMany(cascade = CascadeType.MERGE, targetEntity = FilterColumn.class)
private Set<FilterColumn> columns;
// setters and getters
}
@Entity
public class FilterColumn implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private FilterColumnId id;
private String caption;
// getters and setters
@Embeddable
public static class FilterColumnId implements Serializable {
private static final long serialVersionUID = 1L;
@ManyToOne
private Filter filter;
@Column
private String name;
// getters and setters
}
}
But when I start the application with drop-create instruction the following 3 tables are created: Filter PK(name) FilterColumn PK(filter_name, name) Filter_FilterColumn PK(filter_filter_name, filterColumn_filter_name, filterColumn_name)
What I really want is just two tables like: Filter PK(name) Filter_Column PK(name, filter_name)
Why do I receive this result? Is there something wrong with my mapping? What should I change?
Thanks in advance.
I think you need a mappedBy
on the @OneToMany
. Without that, the mapper doesn't know that it can look at the filtercolumn
table to find the entities associated with a Filter
, so it generates the filter_filtercolumn
table.
Not sure off the top of my head how you to a mappedBy
with a composite key. Given that you're using an @EmbeddedId
, i think it's simply mappedBy = "id"
.
Can you use a @ManyToOne
in a key class like that? Is that a Hibernate extension over and above the JPA spec? Wouldn't you normally need a @MapsId
in there somewhere?
Try adding a @JoinColumn
annotation on the Filter member of your composite id. The actual column would be whatever the id of the of the Filter table is (or just leave it without a name if you let hibernate generate it all).
Let me know if this works as I had a similar problem and solved it using the above so I do know it's possible. The only other thing mine has is a @ForeignKey
annotation but I think hibernate will take care of that for you -- I just did mine because I wanted to stick to a naming convention.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.