0
我有應用程序,它是在MySQL數據庫內存儲解析的URL和填充分析的句子,然後解析的單詞等使用級聯(所以我只保存網址,分貝插入自動休息)。防止行列重複行休眠
在ProcessedUrl POJO我有字段:
Long id
String url
Date date
Set<Sentence> sentences
現在PK是在本機的方式生成的ID。我想達到這樣的目的 - 當用戶輸入一個已經解析並存入數據庫的url時,它不會被再次解析/重複。
什麼是使用hbm.xml
映射實現此目的的合適方法?
編輯:
ProcessedUrl POJO:
public class ProcessedUrl {
private long id;
private String url;
private Date date;
private Set<Sentence> sentences;
public ProcessedUrl() {
}
public ProcessedUrl(String url, Date date) {
this.setUrl(url);
this.setDate(date);
}
public ProcessedUrl(String url, Date date, Set<Sentence> sentences) {
this.setUrl(url);
this.setDate(date);
this.setSentences(sentences);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Set<Sentence> getSentences() {
return this.sentences;
}
public void setSentences(Set<Sentence> sentences) {
this.sentences = sentences;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(!(obj instanceof ProcessedUrl)) return false;
ProcessedUrl that = (ProcessedUrl) obj;
EqualsBuilder eb = new EqualsBuilder();
eb.append(this.getUrl(), that.getUrl());
return eb.isEquals();
}
@Override
public int hashCode() {
HashCodeBuilder hcb = new HashCodeBuilder();
hcb.append(url);
return hcb.toHashCode();
}
}
ProcessedUrl.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="ProcessedUrl">
<id name="id">
<column name="url_id" />
<generator class="native"/>
</id>
<property name="url" type="text"/>
<property name="date" type="java.util.Date" />
<set name="sentences" cascade="all" >
<key column="PROCESSED_URL_ID" />
<one-to-many class="model.Sentence" />
</set>
</class>
</hibernate-mapping>
後完滿成功e代碼ProcessedUrl POJO和當前hbm xml文件? – developer
@javaguy你在這裏。 – Saris