Commit 5a3d80df authored by Mahdi Sellami's avatar Mahdi Sellami
Browse files

Added n*m Relation between Keywords and Subcategories

parent b639b634
package org.tmms.wrapper.api;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
......@@ -35,6 +41,14 @@ public class Subcategory {
@OneToOne
@JoinColumn(name = "CATEGORY_ID")
private Category category;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "sub_category_keyword",
joinColumns = { @JoinColumn(name = "sub_cat_id") },
inverseJoinColumns = { @JoinColumn(name = "keyword_id") }
)
Set<KeyWord> keyWords = new HashSet<>();
/**
* A no-argument constructor.
......@@ -58,6 +72,7 @@ public class Subcategory {
final int prime = 31;
int result = 1;
result = prime * result + ((category == null) ? 0 : category.hashCode());
result = prime * result + ((keyWords == null) ? 0 : keyWords.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + (int) (subCatId ^ (subCatId >>> 32));
return result;
......@@ -77,6 +92,11 @@ public class Subcategory {
return false;
} else if (!category.equals(other.category))
return false;
if (keyWords == null) {
if (other.keyWords != null)
return false;
} else if (!keyWords.equals(other.keyWords))
return false;
if (name == null) {
if (other.name != null)
return false;
......@@ -111,4 +131,12 @@ public class Subcategory {
this.category = category;
}
public Set<KeyWord> getKeyWords() {
return keyWords;
}
public void setKeyWords(Set<KeyWord> keyWords) {
this.keyWords = keyWords;
}
}
......@@ -57,10 +57,15 @@ public class IdeaResource {
if (input.getKeywords()!=null && input.getKeywords().size()>0){
for (String keyword : input.getKeywords()){
List<KeyWord> dbKeyWord = keywordDAO.findByName(keyword);
if (dbKeyWord.size()>0)
if (dbKeyWord.size()>0){
idea.getKeywords().add(dbKeyWord.get(0));
else
idea.getKeywords().add(new KeyWord(keyword));
idea.getSubcategory().getKeyWords().add(dbKeyWord.get(0));
}
else{
KeyWord newKeyWord = new KeyWord(keyword);
idea.getKeywords().add(newKeyWord);
idea.getSubcategory().getKeyWords().add(newKeyWord);
}
}
}
return ideaDAO.insert(idea);
......
......@@ -45,6 +45,14 @@
<constraints nullable="false"/>
</column>
</createTable>
<createTable tableName="sub_category_keyword">
<column name="sub_cat_id" type="bigint">
<constraints nullable="false"/>
</column>
<column name="keyword_id" type="bigint">
<constraints nullable="false"/>
</column>
</createTable>
<createTable tableName="categories">
<column name="cat_id" type="bigint" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
......@@ -75,6 +83,28 @@
onUpdate="RESTRICT"
referencedColumnNames="keyword_id"
referencedTableName="keywords"/>
<addPrimaryKey
columnNames="sub_cat_id, keyword_id"
constraintName="pk_sub_category_keyword"
tableName="sub_category_keyword"/>
<addForeignKeyConstraint baseColumnNames="sub_cat_id"
baseTableName="sub_category_keyword"
constraintName="fk1_sub_category_keyword"
deferrable="false"
initiallyDeferred="false"
onDelete="CASCADE"
onUpdate="RESTRICT"
referencedColumnNames="sub_cat_id"
referencedTableName="sub_categories"/>
<addForeignKeyConstraint baseColumnNames="keyword_id"
baseTableName="sub_category_keyword"
constraintName="fk2_sub_category_keyword"
deferrable="false"
initiallyDeferred="false"
onDelete="CASCADE"
onUpdate="RESTRICT"
referencedColumnNames="keyword_id"
referencedTableName="keywords"/>
<addForeignKeyConstraint baseColumnNames="sub_category_id"
baseTableName="ideas"
constraintName="fk_idea_subcategory"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment