【问题标题】:How to handle embedded composite key?如何处理嵌入的复合键?
【发布时间】:2020-10-12 17:00:47
【问题描述】:

我有一个名为 watchlist 的表,它只包含两个字段,符号和名称。这种组合是独一无二的。我收到休眠错误:

“实体映射中的重复列”

错误,我不知道为什么。

我创建了一个像这样的 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);
}

【问题讨论】:

    标签: java hibernate composite-primary-key


    【解决方案1】:

    使用@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();
        }
    }
            
    

    【讨论】:

    • 谢谢 cdalxndr。这似乎解决了我描述的错误。现在出现一个新错误:为方法 public abstract java.util.List net.tekknow.moneymachine.repository.WatchlistRepository.getWatchlist(java.lang.String) 使用命名参数,但在带注释的查询“选择”中找不到参数“可选 [名称]” * 来自监视列表 w where (name = :w.watchlistId.name)'!
    • @user3217883 你使用了@Param("name"),所以sql参数名必须相同(name)。将:w.watchlistId.name 替换为:name。
    • 确实如此。傻我。非常感谢!多亏像你这样的好心人,我才逐渐掌握了这些复杂的东西。
    猜你喜欢
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多