Announcement Announcement Module
Collapse
No announcement yet.
Problem with EmbeddedId and ManyToOne Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with EmbeddedId and ManyToOne

    Hi I'm new in spring and I have some problems with a composite key and many to one relation.

    My classes are the following.
    Code:
    @Entity
    @Table(name="TEST2_SERVICES")
    public class Test2Service implements Serializable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -407995405967173101L;
    	
    	@EmbeddedId
    	private Test2ServiceId testServiceId;
    	
    	private String description;
    	
    	
    	private @Valid SbService sbServices;
    
    	@Column(name="DESCRIPTION")
    	public String getDescription() {
    		return description;
    	}
    
    	public void setDescription(String description) {
    		this.description = description;
    	}
    
    	@ManyToOne(cascade=CascadeType.MERGE)
    	@JoinColumn(name="SERVICE_ID")
    	public SbService getSbServices() {
    		return sbServices;
    	}
    
    	public void setSbServices(SbService sbServices) {
    		this.sbServices = sbServices;
    	}
    
    }
    With the embedded Id class:

    Code:
    @Embeddable
    public class Test2ServiceId implements Serializable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 6572282224706486208L;
    
    	
    	private String ip;
    	
    	
    	private Integer port;
    
    	
    	
    	public Test2ServiceId() {
    
    	}
    	
    	@Column(name="IP")
    	@NotNull
    	@Size(min=1, max=20)
    	public String getIp() {
    		return ip;
    	}
    
    	public void setIp(String ip) {
    		this.ip = ip;
    	}
    
    	@Column(name="PORT")
    	@NotNull
    	public Integer getPort() {
    		return port;
    	}
    
    	public void setPort(Integer port) {
    		this.port = port;
    	}
    
    	@Override
    	public int hashCode() {
    		return this.ip.hashCode() + this.port;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if(obj instanceof Test2ServiceId){
    			Test2ServiceId sbTicketServiceServiceId = (Test2ServiceId) obj;
    			return sbTicketServiceServiceId.port == this.port && sbTicketServiceServiceId.ip.equals(this.ip);
    		}
    		return false;
    	}
    }
    And the other side of the relation is:

    Code:
    @Entity
    @Table(name="SERVICES")
    public class SbService implements Serializable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 3848055773985685841L;
    
    	private String id;
    	
    	private String description;
    	
    	private SbServiceType sbServiceType;
    	
    	private Set<Test2Service> test2Services = new HashSet<Test2Service>();
    	
    	@Id
    	@Column(name="SERVICE_ID")
    	@NotNull
    	@Size(min=2, max=6)
    	public String getId() {
    		return id;
    	}
    
    	public void setId(String id) {
    		this.id = id;
    	}
    
    
    	@Column(name="DESCRIPTION")
    	@Size(max=100)
    	public String getDescription() {
    		return description;
    	}
    
    	public void setDescription(String description) {
    		this.description = description;
    	}
    	
    	@ManyToOne
    	@JoinColumn(name="SERVICE_TYPE_ID")
    	public SbServiceType getSbServiceType() {
    		return sbServiceType;
    	}
    
    	public void setSbServiceType(SbServiceType sbServiceType) {
    		this.sbServiceType = sbServiceType;
    	}
    
    	
    	@OneToMany(mappedBy="sbServices", cascade=CascadeType.MERGE)
    	public Set<Test2Service> getTest2Services() {
    		return test2Services;
    	}
    
    	public void setTest2Services(Set<Test2Service> test2Services) {
    		this.test2Services = test2Services;
    	}
    
    	@Override
    	public String toString() {
    		return "Id: " + this.getId() + ", tipo: " + this.getSbServiceType().getType() + ", Desc:  " + this.getDescription();
    	}
    	
    	@Transient
    	public String getSelectString(){
    		return this.getId() + " " + this.getDescription();
    	}
    
    }
    I create a repository interface that extends from

    Code:
    public interface Test2ServiceRepository extends PagingAndSortingRepository<Test2Service, Test2ServiceId>{
    }

    The repository is scanned when the application start with the following
    Code:
     <jpa:repositories base-package="com.testing.repository"  entity-manager-factory-ref="emf" transaction-manager-ref="transactionManager"/>
    Now I can use that interface with the @Autowired. But my problem presents when I do this.test2ServiceRepository.findAll()

    The following error presents:

    org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceU sageException: could not execute query; SQL [select test2servi0_.ip as ip8_, test2servi0_.port as port8_, test2servi0_.description as descript3_8_, test2servi0_.sbServices as sbServices8_ from TEST2_SERVICES test2servi0_]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
    I have done some testing without the many to one relation and is working fine. But adding that it breaks the findall

    Any help will be welcome!

    Regards
Working...
X