0

I have an entity: HtmlElement with below @OneToOne relationship with entity: Component

QueryParameter Entity:

@Id
@Column(name = "QUERY_PARAMETER_ID")
private int queryParameterId;

@Column(name = "QUERY_PARAMETER")
private String queryParameter;  

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "QUERY_ID")  
private Query Query;    

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "HTML_ELEMENT_ID")
private HtmlElement HtmlElement;

HtmlElement Entity:

@Id
@Column(name = "HTML_ELEMENT_ID")
private int htmlElementId;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "COMPONENT_ID")
private Component Component;

@Column(name = "HTML_ELEMENT_ID_NAME")
private String htmlElementIdName;

@Column(name = "HTML_ELEMENT_SHOWING_NAME")
private String htmlElementShowingName;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "TYPE_ID")
private Type Type;

@Column(name = "HTML_ELEMENT_MAXLENGTH")
private String htmlElementMaxlength;

@Column(name = "HTML_ELEMENT_PLACEHOLDER")
private String htmlElementPlaceholder;

@Column(name = "HTML_ELEMENT_HELPTEXT")
private String htmlElementHelptext;

@OneToOne(mappedBy = "HtmlElement")
private QueryParameter QueryParameter;

Component Entity:

@Id
@Column(name = "COMPONENT_ID")
private int componentId;

private String component;

@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
    schema = "APP_OWNER",
    name = "_COMPONENT_TYPE",
    joinColumns = { @JoinColumn(name = "COMPONENT_ID") },
    inverseJoinColumns = { @JoinColumn(name = "TYPE_ID") }
)
@OrderBy("TYPE_NAME")
private Set<Type> Types = new HashSet<>();

@OneToOne(mappedBy = "Component")
private HtmlElement HtmlElement;

Type Entity:

@Id
@Column(name = "TYPE_ID")
private int typeId;

@Column(name = "TYPE_NAME")
private String typeName;

@ManyToMany(mappedBy = "Types")
private Set<Component> Components = new HashSet<>();

@OneToOne(mappedBy = "Type")
private HtmlElement HtmlElement;

Basically the relationship here means that one html element has one component related to it.

Now, when I want to find all components like below:

List<Component> componentList = componentRepo.findAllByOrderByComponentIdAsc();

It's giving me the following error: More than one row with the given identifier was found: 3

If I run the generated query it returns 3 rows, but that doesn't make any sense since Component has the different html components(input, select, etc) and HtmlElement is using the component Id and more information related to that, it is perfectly fine to re-use the same Component ID in different HtmlElements.

I tried cascading all and deleting orphan as suggested in here but didn't work, as well as the other suggestions in the same page.

Any idea on how to fix that please?

Somebody
  • 2,667
  • 14
  • 60
  • 100
  • when you use the same componentId in different HtmlElements you don't have a @OneToOne relation – FredvN Jan 06 '20 at 16:30
  • Can you add the whole entity? I think this could be related with the entities: type, query_parameter or query .. Could you check the mapping on then or post here as well? – Brother Jan 06 '20 at 16:30
  • @Brother I've posted all the entities now. – Somebody Jan 06 '20 at 16:49

1 Answers1

2

As per the Description ("it is perfectly fine to re-use the same Component ID in different HtmlElements") same component is used in different HtmlElement, so the relationship should be ManyToOne not OneToOne. The HtmlElement entity should be as mentioned below:

@ManyToOne
@JoinColumn(name = "COMPONENT_ID")
private Component component;

PS: Don't use cascade = CascadeType.ALL because if one HTMLElement removed the associated component will also removed that is linked with other HTMLElement.

  • indeed this is a ManyToOne as you suggested and a OneToMany with a collection in the other end, now is working. – Somebody Jan 06 '20 at 20:09