当我在eclipse中运行我的项目上的hibernate工具架构导出时,我得到以下错误,似乎它说我正在映射相同的集合,但我不是。有没有任何解决方案,是否有我应该使用的注释,或在配置文件中指定集合不相同的任何方式?我非常感谢有关如何解决此问题的任何建议或建议。以下是错误的代码和堆栈跟踪。如果您需要更多信息,请告诉我。
org.hibernate.DuplicateMappingException: Duplicate collection role mapping entity.AssignedTeams.assignedTeams
at org.hibernate.cfg.Configuration$MappingsImpl.addCollection(Configuration.java:2439)
at org.hibernate.cfg.annotations.CollectionBinder.bind(CollectionBinder.java:511)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1906)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:769)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:733)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:636)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:359)
at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:838)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:128)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:91)
at org.hibernate.eclipse.console.views.SchemaExportAction$1.execute(SchemaExportAction.java:104)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:107)
at org.hibernate.eclipse.console.views.SchemaExportAction.doRun(SchemaExportAction.java:91)
at org.hibernate.eclipse.console.actions.ConsoleConfigurationBasedAction.run(ConsoleConfigurationBasedAction.java:65)
at org.eclipse.ui.actions.BaseSelectionListenerAction.runWithEvent(BaseSelectionListenerAction.java:168)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="connection.url">jdbc:derby://localhost:1527/jtracker</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<!-- <property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -->
<!-- Enable the second-level cache -->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Import the mapping resources -->
<mapping class="entity.AssignedTeams" />
<mapping class="entity.AssignedPersonnel" />
<mapping class="entity.Comments" />
<mapping class="entity.Created" />
<mapping class="entity.Modified" />
<mapping class="entity.Priority" />
<mapping class="entity.RelatedTickets" />
<mapping class="entity.Request" />
<mapping class="entity.Ticket" />
<mapping class="entity.TicketDescription" />
<mapping class="entity.TicketName" />
<mapping class="entity.User" />
</session-factory>
</hibernate-configuration>
课程:
package abstract_classes;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@MappedSuperclass
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Field {
private int id;
public abstract int getSize();
public abstract void update(HttpServletRequest request, HttpServletResponse response);
/**
* @return the id
*/
@Id
@GeneratedValue
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
package entity;
import java.util.ArrayList;
import java.util.List;
import abstract_classes.Field;
import javax.persistence.Cacheable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class AssignedPersonnel extends Field{
private List<AssignedPersonnelElement> assignedPersonnel;
public AssignedPersonnel(){
assignedPersonnel = new ArrayList<AssignedPersonnelElement>();
}
public void addPersonnel(AssignedPersonnelElement personnel){
assignedPersonnel.add(personnel);
}
public void removePersonnel(AssignedPersonnelElement personnel){
assignedPersonnel.remove(personnel);
}
@Override
public int getSize() {
return assignedPersonnel.size();
}
@ElementCollection(fetch= FetchType.LAZY )
@JoinTable (name="ASGN_PERSNL_LIST",joinColumns=@JoinColumn(name="ASGN_PERSNL_ID"))
public List<AssignedPersonnelElement> getAssignedPersonnel() {
return assignedPersonnel;
}
public void setAssignedPersonnel(List<AssignedPersonnelElement> assignedPersonnel) {
this.assignedPersonnel = assignedPersonnel;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "AssignedPersonnel [assignedPersonnel=" + assignedPersonnel
+ "]";
}
@Override
public void update(HttpServletRequest request,
HttpServletResponse response) {
String[] assignedPersonnel = request.getParameterValues("assignedPersonnel");
for (int i = 0; i < assignedPersonnel.length; i++){
if(!(this.assignedPersonnel.contains(new AssignedPersonnelElement(assignedPersonnel[i])))){
this.assignedPersonnel.add(new AssignedPersonnelElement(assignedPersonnel[i]));
}
}
}
}
package entity;
import java.util.ArrayList;
import java.util.List;
import abstract_classes.Field;
import javax.persistence.Cacheable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class AssignedTeams extends Field{
private List<AssignedTeamElement> assignedTeams;
public AssignedTeams(){
assignedTeams = new ArrayList<AssignedTeamElement>();
}
public void addTeam(AssignedTeamElement team){
assignedTeams.add(team);
}
public void removeTeam(AssignedTeamElement team){
assignedTeams.remove(team);
}
@Override
public int getSize() {
return assignedTeams.size();
}
@ElementCollection(fetch= FetchType.LAZY)
@JoinTable (name="ASGN_TEAM_LIST",joinColumns=@JoinColumn(name="ASGN_TEAM_ID"))
public List<AssignedTeamElement> getAssignedTeams() {
return assignedTeams;
}
public void setAssignedTeams(List<AssignedTeamElement> assignedTeams) {
this.assignedTeams = assignedTeams;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "AssignedTeams [assignedTeams=" + assignedTeams + "]";
}
@Override
public void update(HttpServletRequest request,
HttpServletResponse response) {
String[] assignedTeams = request.getParameterValues("assignedTeams");
for (int i = 0; i < assignedTeams.length; i++){
if(!(this.assignedTeams.contains(new AssignedTeamElement(assignedTeams[i])))){
this.assignedTeams.add(new AssignedTeamElement(assignedTeams[i]));
}
}
}
}
package entity;
import java.util.ArrayList;
import java.util.List;
import abstract_classes.Field;
import javax.persistence.Cacheable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class RelatedTickets extends Field{
private List<RelatedTicketElement> relatedTickets;
public RelatedTickets() {
relatedTickets = new ArrayList<RelatedTicketElement>();
}
public void addRelatedTicket(RelatedTicketElement ticket){
relatedTickets.add(ticket);
}
public void removeRelatedTicket(RelatedTicketElement ticket){
relatedTickets.remove(ticket);
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return relatedTickets.size();
}
@ElementCollection(fetch= FetchType.LAZY)
@JoinTable (name="REL_TICKT_LIST",joinColumns=@JoinColumn(name="REL_TICKT_ID"))
public List<RelatedTicketElement> getRelatedTickets() {
return relatedTickets;
}
public void setRelatedTickets(List<RelatedTicketElement> relatedTickets) {
this.relatedTickets = relatedTickets;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "RelatedTickets [relatedTickets=" + relatedTickets + "]";
}
@Override
public void update(HttpServletRequest request,
HttpServletResponse response) {
String[] relatedTickets = request.getParameterValues("relatedTickets");
for (int i = 0; i < relatedTickets.length; i++){
if(!(this.relatedTickets.contains(new RelatedTicketElement(relatedTickets[i])))){
this.relatedTickets.add(new RelatedTicketElement(relatedTickets[i]));
}
}
}
}
package entity;
import javax.persistence.Embeddable;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Embeddable
public class AssignedPersonnelElement {
private String assignedPersonnel;
private int id;
public AssignedPersonnelElement() {}
public AssignedPersonnelElement(String assignedPersonnel) {
super();
this.assignedPersonnel = assignedPersonnel;
}
public String getAssignedPersonnel() {
return assignedPersonnel;
}
public void setAssignedPersonnel(String assignedPersonnel) {
this.assignedPersonnel = assignedPersonnel;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime
* result
+ ((assignedPersonnel == null) ? 0 : assignedPersonnel
.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof AssignedPersonnelElement))
return false;
AssignedPersonnelElement other = (AssignedPersonnelElement) obj;
if (assignedPersonnel == null) {
if (other.assignedPersonnel != null)
return false;
} else if (!assignedPersonnel.equals(other.assignedPersonnel))
return false;
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "AssignedPersonnelElement [assignedPersonnel="
+ assignedPersonnel + "]";
}
}
package entity;
import javax.persistence.Embeddable;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Embeddable
public class AssignedTeamElement {
private String assignedTeam;
private int id;
public AssignedTeamElement() {
}
public AssignedTeamElement(String assignedTeam) {
this.assignedTeam = assignedTeam;
}
public String getAssignedTeam() {
return assignedTeam;
}
public void setAssignedTeam(String assignedTeam) {
this.assignedTeam = assignedTeam;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((assignedTeam == null) ? 0 : assignedTeam.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof AssignedTeamElement))
return false;
AssignedTeamElement other = (AssignedTeamElement) obj;
if (assignedTeam == null) {
if (other.assignedTeam != null)
return false;
} else if (!assignedTeam.equals(other.assignedTeam))
return false;
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "AssignedTeamElement [assignedTeam=" + assignedTeam + "]";
}
}
package entity;
import javax.persistence.Embeddable;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Embeddable
public class RelatedTicketElement {
private String relatedTicket;
private int id;
public RelatedTicketElement() {}
public RelatedTicketElement(String relatedTicket) {
super();
this.relatedTicket = relatedTicket;
}
public String getRelatedTicket() {
return relatedTicket;
}
public void setRelatedTicket(String relatedTicket) {
this.relatedTicket = relatedTicket;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((relatedTicket == null) ? 0 : relatedTicket.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof RelatedTicketElement))
return false;
RelatedTicketElement other = (RelatedTicketElement) obj;
if (relatedTicket == null) {
if (other.relatedTicket != null)
return false;
} else if (!relatedTicket.equals(other.relatedTicket))
return false;
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "RelatedTicketElement [relatedTicket=" + relatedTicket + "]";
}
}
答案 0 :(得分:0)
问题是您忘记了<generator class="increment"/>
课程中id
的{{1}}。
所以试试这个:
Field