如何处理嵌套的复合键?

huangapple go评论72阅读模式
英文:

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=&quot;symbol&quot;)
	private String symbol;
	
	@Column(name=&quot;name&quot;)
	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) &amp;&amp; Objects.equals(name, that.name);
    }
 
    @Override
    public int hashCode() {
        return Objects.hash(symbol, name);
    }	

The Watchlist class looks like this:

@Entity
@Table(name = &quot;watchlist&quot;)
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&lt;Watchlist, WatchlistId&gt;{
    @Query(value=&quot;select * from watchlist w where (name = :w.watchlistId.name)&quot;, nativeQuery = true)
    List&lt;Watchlist&gt; getWatchlist(@Param(&quot;name&quot;) String name);

	@Query(value=&quot;insert into watchlist w (symbol,name) values (:watchlist.watchlistId.symbol,:watchlist.watchlistId.name)&quot;, nativeQuery = true)
	String addSymbolToWatchlist(@Param(&quot;watchlist&quot;) Watchlist watchlist);
}

答案1

得分: 0

使用 @Embeddable 时,嵌入类中的字段会自动添加到表中,因此会出现重复。

因此,您需要移除 symbolname 字段,并从 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 = &quot;watchlist&quot;)
public class Watchlist {
    @EmbeddedId
    public WatchlistId watchlistId;
    
    public String getSymbol(){
        return watchlistId.getSymbol();
    }
    
    public String getName(){
        return watchlistId.getName();
    }
}

huangapple
  • 本文由 发表于 2020年10月13日 01:00:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64322160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定