-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: onetomany unique contraint
PostPosted: Tue May 13, 2008 12:01 pm 
Newbie

Joined: Tue Apr 22, 2008 4:11 am
Posts: 9
hi!

i try to map a locationtype to a location! but if i add a location holding the same locationtype-foreignkey in the table i get an exception! i tried to set the attribute unique=false at the joincolumn annotation but this didn't work!


help please...

Code:
@Entity
public class Location {

        @OneToOne
   @JoinColumn(name="locationType_fk")
   public LocationType getLocationType() {
      return locationType;
   }
   public void setLocationType(LocationType locationType) {
      this.locationType = locationType;
   }
}

@Entity
public class LocationType {

       @OneToOne(mappedBy = "locationType")
   public Location getLocation() {
      return location;
   }

   public void setLocation(Location location) {
      this.location = location;
   }
}


Full stack trace of any exception that occurs:


org.hibernate.HibernateException: More than one row with the given identifier was found: 2, for class: com.stereoscope.entity.Location
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:69)
at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1645)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:608)
at org.hibernate.type.EntityType.resolve(EntityType.java:382)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
at org.hibernate.type.EntityType.resolve(EntityType.java:379)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64)
at com.stereoscope.session.LocationHome.getLocations(LocationHome.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
at org.jboss.seam.persistence.ManagedEntityIdentityInterceptor.aroundInvoke(ManagedEntityIdentityInterceptor.java:48)
at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:38)
at org.jboss.seam.util.Work.workInTransaction(Work.java:40)
at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
at com.stereoscope.session.LocationHome_$$_javassist_6.getLocations(LocationHome_$$_javassist_6.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
at org.jboss.seam.Component.callComponentMethod(Component.java:2082)
at org.jboss.seam.Component.getInstanceFromFactory(Component.java:1926)
at org.jboss.seam.Component.getInstance(Component.java:1863)
at org.jboss.seam.Component.getInstance(Component.java:1840)
at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55)
at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50)
at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:166)
at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:53)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:64)
at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
at org.jboss.el.parser.AstEmpty.getValue(AstEmpty.java:29)
at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
at javax.faces.component.UIComponentBase.isRendered(UIComponentBase.java:370)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:880)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:137)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:282)
at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:199)
at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:194)
at org.ajax4jsf.renderkit.RendererBase.encodeChildren(RendererBase.java:121)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:216)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:307)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:619)

Name and version of the database you are using:
hsqldb 1.8


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 3:53 pm 
Beginner
Beginner

Joined: Wed Mar 05, 2008 4:57 am
Posts: 22
Location: Bangalore,India
As per the exception, it seems that in Database you already have a record for Location object with identifier 2.

Please provide the XML mapping and the Java code that inserts the records for more accurate answer.


But as per my understanding of the scenario, many Locations can be of same LocationType. So <one-to-one> mapping between Location and LocationType may not be a good idea.

Try to have the association like

LocationType........<one-to-many>.......Location

_________________
Naresh Waswani
+91-9986461501


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 2:08 am 
Newbie

Joined: Tue Apr 22, 2008 4:11 am
Posts: 9
yeah ! your right there can be different locations of the same locationtype!

so could you describe me please how to come along with this issue !? must there be a list of locations on the locationtype site ?

thanks for your help

Code:
insert into locationtype (id, locationtype_en,locationtype_de) values (1, 'Bar', 'Bar');
insert into locationtype (id, locationtype_en,locationtype_de) values (2, 'Cafe', 'Cafe');
insert into locationtype (id, locationtype_en,locationtype_de) values (3, 'Restaurant', 'Restaurant');
insert into locationtype (id, locationtype_en,locationtype_de) values (4, 'Inn', 'Wirtshaus');
insert into locationtype (id, locationtype_en,locationtype_de) values (5, 'Club', 'Club');
insert into locationtype (id, locationtype_en,locationtype_de) values (6, 'Pub', 'Pub');

Insert into address (id, countrynamecode, administrativeareaname, subadministrativeareaname, localityname,  thoroughfarename, postalcodenumber, lat, lng) values (1, 'AT', 'Steiermark', 'Graz (Stadt)', 'Graz', 'Trauttmansdorffgasse 3', '8010', 47.070944, 15.442943);
insert into address (id, countrynamecode, administrativeareaname, subadministrativeareaname, localityname,  thoroughfarename, postalcodenumber, lat, lng) values (2, 'AT', 'Steiermark', 'Graz (Stadt)', 'Graz', 'Freiheitsplatz 2', '8010', 47.072858, 15.440779);
insert into address (id, countrynamecode, administrativeareaname, subadministrativeareaname, localityname,  thoroughfarename, postalcodenumber, lat, lng) values (3, 'AT', 'Steiermark', 'Graz (Stadt)', 'Graz', 'Neubaugasse 6', '8020', 47.075721, 15.431178);

insert into location (id, name, description, address_fk, locationType_fk) values (1, 'The office pub', 'A fine pub', 1, 6);
insert into location (id, name, description, address_fk, locationType_fk) values (2, 'Cafe Fink', 'A nice cafe', 2, 2);
insert into location (id, name, description, address_fk, locationType_fk) values (3, 'p.p.c', 'Project Pop Culture', 3, 5);



there are the three entities! the instert works fine but if i want to list the content of the location tabel on my page i get this error from above!

Code:
package com.stereoscope.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import org.hibernate.validator.NotNull;

@Entity
public class Location {

   private Long id;
   private String name;
   private Address address;
   private String description;
   private LocationType locationType;
   
   @Id
   @GeneratedValue
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   
   @NotNull
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name="address_fk")
   public Address getAddress() {
      return address;
   }
   public void setAddress(Address address) {
      this.address = address;
   }
   
   @Column(nullable=true)
   public String getDescription() {
      return description;
   }
   public void setDescription(String description) {
      this.description = description;
   }
   @OneToOne
   @JoinColumn(name="locationType_fk")
   public LocationType getLocationType() {
      return locationType;
   }
   public void setLocationType(LocationType locationType) {
      this.locationType = locationType;
   }
   
   @Override
   public String toString() {
      return  "Location:      "             + "\n "+
            "Name:          " + name       + "\n" +
            "Description:  " + description    + "\n" +
            "LocationType: " + locationType + "\n" +
            "Address:       " + address;
   }
   
   
}

package com.stereoscope.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class LocationType {

   private Long id;
   private Location location;
   private String locationType_de;
   private String locationType_en;

   public LocationType() {

   }
   
   @Id
   @GeneratedValue
   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getLocationType_de() {
      return locationType_de;
   }

   public void setLocationType_de(String locationType_de) {
      this.locationType_de = locationType_de;
   }

   public String getLocationType_en() {
      return locationType_en;
   }

   public void setLocationType_en(String locationType_en) {
      this.locationType_en = locationType_en;
   }

   @OneToOne(mappedBy = "locationType")
   public Location getLocation() {
      return location;
   }

   public void setLocation(Location location) {
      this.location = location;
   }
   
   public String toString() {
      return    "Location        " +
            "Locationtype DE " + locationType_de + " " +
            "Locationtype EN " + locationType_en;
   }

}

package com.stereoscope.entity;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Address {

   /**
    *  countrynamecode : is a 2 digit code for international representation
    *    administrativeareaname: represents a province (Striermark, Kärnten)
    *    subadministrativeareaname: represents a city (graz)
    *  localityname: represents a county (Weinitzen)
    *  thoroughfarename: represents the street (Zösenberg)
    *  postalcodenumber: represents the zipcode (8045)
    */
   
   private Long id;
   private String countryNameCode;
    private String administrativeAreaName;
    private String subAdministrativeAreaName;
    private String localityName;
    private String thoroughfareName;
    private String postalCodeNumber;
    private GLatLng coordinates;
//    private String housenumber;
    private User user;
    private Location location;
   
    @Id
    @GeneratedValue
    public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
    public String getCountryNameCode() {
      return countryNameCode;
   }
   public void setCountryNameCode(String countryNameCode) {
      this.countryNameCode = countryNameCode;
   }
   public String getAdministrativeAreaName() {
      return administrativeAreaName;
   }
   public void setAdministrativeAreaName(String administrativeAreaName) {
      this.administrativeAreaName = administrativeAreaName;
   }
   public String getSubAdministrativeAreaName() {
      return subAdministrativeAreaName;
   }
   public void setSubAdministrativeAreaName(String subAdministrativeAreaName) {
      this.subAdministrativeAreaName = subAdministrativeAreaName;
   }
   public String getLocalityName() {
      return localityName;
   }
   public void setLocalityName(String localityName) {
      this.localityName = localityName;
   }
   public String getThoroughfareName() {
      return thoroughfareName;
   }
   public void setThoroughfareName(String thoroughfareName) {
      this.thoroughfareName = thoroughfareName;
   }
   public String getPostalCodeNumber() {
      return postalCodeNumber;
   }
   public void setPostalCodeNumber(String postalCodeNumber) {
      this.postalCodeNumber = postalCodeNumber;
   }
   @Embedded
   public GLatLng getCoordinates() {
      return coordinates;
   }
   public void setCoordinates(GLatLng coordinates) {
      this.coordinates = coordinates;
   }
//   public String getHousenumber() {
//      return housenumber;
//   }
//   public void setHousenumber(String housenumber) {
//      this.housenumber = housenumber;
//   }
   @OneToOne(mappedBy = "address")
   public User getUser() {
      return user;
   }
   public void setUser(User user) {
      this.user = user;
   }
   
   @OneToOne(mappedBy = "address")
   public Location getLocation() {
      return location;
   }
   public void setLocation(Location location) {
      this.location = location;
   }
   
   public String toString() {
      return  "Address:               "                         + "\n " +
            "CountryNameCode         " + countryNameCode          + "\n " +
             "AdministrativeAreaName      " + administrativeAreaName       + "\n " +
            "SubAdministrativeAreaName   " + subAdministrativeAreaName    + "\n " +
            "LocalityName            " + localityName              + "\n " +
            "ThoroughfareName         " + thoroughfareName          + "\n " +
            "PostalCodeNumber         " + postalCodeNumber          + "\n " +
            "Coordinates            " + coordinates;
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.