I am generating my Annotated Code Classes with hibernate and eclipse JBoss plugin, For the One to Many & Many to Many Relationships etc, the code generated used Set type for collections and generate many to many as one to many relation.
Project:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Project generated by hbm2java
*/
@Entity
@Table(name = "project", schema = "public")
public class Project implements java.io.Serializable
{
private int projectId;
private Type type;
private Customer customer;
private String projectName;
private String description;
private Double totalContractAmount;
private Set projectConstructionMaterials = new HashSet( 0 );
public Project()
{
}
public Project(int projectId)
{
this.projectId = projectId;
}
public Project(int projectId, Type type, Customer customer, String projectName, String description,
Double totalContractAmount,
Set projectConstructionMaterials)
{
this.projectId = projectId;
this.type = type;
this.customer = customer;
this.projectName = projectName;
this.description = description;
this.totalContractAmount = totalContractAmount;
this.projectConstructionMaterials = projectConstructionMaterials;
}
@Id
@Column(name = "project_id", unique = true, nullable = false)
public int getProjectId()
{
return this.projectId;
}
public void setProjectId(int projectId)
{
this.projectId = projectId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "type_id")
public Type getType()
{
return this.type;
}
public void setType(Type type)
{
this.type = type;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
public Customer getCustomer()
{
return this.customer;
}
public void setCustomer(Customer customer)
{
this.customer = customer;
}
@Column(name = "project_name")
public String getProjectName()
{
return this.projectName;
}
public void setProjectName(String projectName)
{
this.projectName = projectName;
}
@Column(name = "description", length = 500)
public String getDescription()
{
return this.description;
}
public void setDescription(String description)
{
this.description = description;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "project") //it should be many to many annotation
public Set getProjectConstructionMaterials()
{
return this.projectConstructionMaterials;
}
public void setProjectConstructionMaterials(Set projectConstructionMaterials)
{
this.projectConstructionMaterials = projectConstructionMaterials;
}
}
ProjectConstructionMaterial
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* ProjectConstructionMaterial generated by hbm2java
*/
@Entity
@Table(name = "project_construction_material", schema = "public")
public class ProjectConstructionMaterial implements java.io.Serializable
{
private int projectConstructionMaterialId;
private ConstructionMaterialInventory constructionMaterialInventory;
private Project project;
private String description;
private String quantityArrived;
private String quantityConsumed;
public ProjectConstructionMaterial()
{
}
public ProjectConstructionMaterial(int projectConstructionMaterialId)
{
this.projectConstructionMaterialId = projectConstructionMaterialId;
}
public ProjectConstructionMaterial(int projectConstructionMaterialId,
ConstructionMaterialInventory constructionMaterialInventory, Project project, String description,
String quantityArrived, String quantityConsumed)
{
this.projectConstructionMaterialId = projectConstructionMaterialId;
this.constructionMaterialInventory = constructionMaterialInventory;
this.project = project;
this.description = description;
this.quantityArrived = quantityArrived;
this.quantityConsumed = quantityConsumed;
}
@Id
@Column(name = "project_construction_material_id", unique = true, nullable = false)
public int getProjectConstructionMaterialId()
{
return this.projectConstructionMaterialId;
}
public void setProjectConstructionMaterialId(int projectConstructionMaterialId)
{
this.projectConstructionMaterialId = projectConstructionMaterialId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "construction_material_id")
public ConstructionMaterialInventory getConstructionMaterialInventory()
{
return this.constructionMaterialInventory;
}
public void setConstructionMaterialInventory(ConstructionMaterialInventory constructionMaterialInventory)
{
this.constructionMaterialInventory = constructionMaterialInventory;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "project_id")
public Project getProject()
{
return this.project;
}
public void setProject(Project project)
{
this.project = project;
}
@Column(name = "description", length = 500)
public String getDescription()
{
return this.description;
}
public void setDescription(String description)
{
this.description = description;
}
@Column(name = "quantity_arrived")
public String getQuantityArrived()
{
return this.quantityArrived;
}
public void setQuantityArrived(String quantityArrived)
{
this.quantityArrived = quantityArrived;
}
@Column(name = "quantity_consumed")
public String getQuantityConsumed()
{
return this.quantityConsumed;
}
public void setQuantityConsumed(String quantityConsumed)
{
this.quantityConsumed = quantityConsumed;
}
}
ConstructionMaterialInventory
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* ConstructionMaterialInventory generated by hbm2java
*/
@Entity
@Table(name = "construction_material_inventory", schema = "public")
public class ConstructionMaterialInventory implements java.io.Serializable
{
private int constructionMaterialId;
private Type type;
private SubType subType;
private String quantity;
private String description;
private Set projectConstructionMaterials = new HashSet( 0 );
public ConstructionMaterialInventory()
{
}
public ConstructionMaterialInventory(int constructionMaterialId)
{
this.constructionMaterialId = constructionMaterialId;
}
public ConstructionMaterialInventory(int constructionMaterialId, Type type, SubType subType, String quantity,
String description, Set projectConstructionMaterials)
{
this.constructionMaterialId = constructionMaterialId;
this.type = type;
this.subType = subType;
this.quantity = quantity;
this.description = description;
this.projectConstructionMaterials = projectConstructionMaterials;
}
@Id
@Column(name = "construction_material_id", unique = true, nullable = false)
public int getConstructionMaterialId()
{
return this.constructionMaterialId;
}
public void setConstructionMaterialId(int constructionMaterialId)
{
this.constructionMaterialId = constructionMaterialId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "type_id")
public Type getType()
{
return this.type;
}
public void setType(Type type)
{
this.type = type;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sub_type_id")
public SubType getSubType()
{
return this.subType;
}
public void setSubType(SubType subType)
{
this.subType = subType;
}
@Column(name = "quantity")
public String getQuantity()
{
return this.quantity;
}
public void setQuantity(String quantity)
{
this.quantity = quantity;
}
@Column(name = "description", length = 500)
public String getDescription()
{
return this.description;
}
public void setDescription(String description)
{
this.description = description;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "constructionMaterialInventory")
public Set getProjectConstructionMaterials()
{
return this.projectConstructionMaterials;
}
public void setProjectConstructionMaterials(Set projectConstructionMaterials)
{
this.projectConstructionMaterials = projectConstructionMaterials;
}
}
Data Model
I want to change it to many to many
annotations and also want to use List
or ArrayList
instead of Set
. Can anyone please tell me how to do this with automatically code generation? Beause i have dozens of table like this and change in tables manually takes too much time..
For Example: Annotation Must be generate automatically as described in this example. Many to Many annotation Example