英文:
How to handle embedded composite key?
问题
我有一个名为watchlist的表,只包含两个字段,symbol和name。这个组合是唯一的。我遇到了Hibernate错误:
> "实体的映射中重复的列"
错误,我无法弄清楚原因。
我创建了一个类WatchlistId,如下所示:
@Embeddable
public class WatchlistId implements Serializable {
@Column(name="symbol")
private String symbol;
@Column(name="name")
private String name;
WatchlistId() {
}
WatchlistId(String symbol, String name) {
this.symbol = symbol;
this.name = name;
}
// getters and setters
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WatchlistId that = (WatchlistId) o;
return Objects.equals(symbol, that.symbol) && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(symbol, name);
}
}
Watchlist类如下所示:
@Entity
@Table(name = "watchlist")
public class Watchlist {
@EmbeddedId
public WatchlistId watchlistId;
private String symbol;
private String name;
public Watchlist() {
}
public Watchlist(WatchlistId watchlistId, String symbol, String name) {
this.watchlistId = watchlistId;
this.symbol = symbol;
this.name = name;
}
public WatchlistId getWatchlistId() {
return watchlistId;
}
public void setWatchlistId(WatchlistId watchlistId) {
this.watchlistId = watchlistId;
}
}
仓库类如下所示:
@Repository
public interface WatchlistRepository extends JpaRepository<Watchlist, WatchlistId> {
@Query(value="select * from watchlist w where (name = :w.watchlistId.name)", nativeQuery = true)
List<Watchlist> getWatchlist(@Param("name") String name);
@Query(value="insert into watchlist w (symbol,name) values (:watchlist.watchlistId.symbol,:watchlist.watchlistId.name)", nativeQuery = true)
String addSymbolToWatchlist(@Param("watchlist") Watchlist watchlist);
}
英文:
I have a table called watchlist that consists of only two fields, symbol and name. The combination is unique. I'm getting hibernate error:
> "Repeated column in mapping for entity"
error and I can't figure out why.
I have created a WatchlistId class like this:
@Embeddable
public class WatchlistId implements Serializable {
@Column(name="symbol")
private String symbol;
@Column(name="name")
private String name;
WatchlistId() {
}
WatchlistId(String symbol, String name) {
this.symbol = symbol;
this.name = name;
}
//getters and setters
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WatchlistId that = (WatchlistId) o;
return Objects.equals(symbol, that.symbol) && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(symbol, name);
}
The Watchlist class looks like this:
@Entity
@Table(name = "watchlist")
public class Watchlist {
@EmbeddedId
public WatchlistId watchlistId;
private String symbol;
private String name;
public Watchlist() {
}
public Watchlist(WatchlistId watchlistId, String symbol, String name) {
this.watchlistId = watchlistId;
this.symbol = symbol;
this.name = name;
}
public WatchlistId getWatchlistId() {
return watchlistId;
}
public void setWatchlistId(WatchlistId watchlistId) {
this.watchlistId = watchlistId;
}
}
The repository looks like this:
@Repository
public interface WatchlistRepository extends JpaRepository<Watchlist, WatchlistId>{
@Query(value="select * from watchlist w where (name = :w.watchlistId.name)", nativeQuery = true)
List<Watchlist> getWatchlist(@Param("name") String name);
@Query(value="insert into watchlist w (symbol,name) values (:watchlist.watchlistId.symbol,:watchlist.watchlistId.name)", nativeQuery = true)
String addSymbolToWatchlist(@Param("watchlist") Watchlist watchlist);
}
答案1
得分: 0
使用 @Embeddable
时,嵌入类中的字段会自动添加到表中,因此会出现重复。
因此,您需要移除 symbol
和 name
字段,并从 WatchlistId
成员中使用它们:
@Entity
@Table(name = "watchlist")
public class Watchlist {
@EmbeddedId
public WatchlistId watchlistId;
public String getSymbol(){
return watchlistId.getSymbol();
}
public String getName(){
return watchlistId.getName();
}
}
英文:
When using @Embeddable
, the fields from the embeddable class are automatically added to the table, so you get duplicates.
Thus, you need to remove symbol
and name
fields and use them from WatchlistId
member:
@Entity
@Table(name = "watchlist")
public class Watchlist {
@EmbeddedId
public WatchlistId watchlistId;
public String getSymbol(){
return watchlistId.getSymbol();
}
public String getName(){
return watchlistId.getName();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论