Как создать композитный первичный ключ в спящем JPA? - PullRequest
1 голос
/ 19 марта 2012

Я пытаюсь создать составной первичный ключ в таблице из 2 внешних ключей, используя hibernate JPA, но выдает ошибку. Я нахожу в сети решение , но ничего

Отношения между таблицами выглядят так:

   Table Client

    clientID(PK)

    FirstName

    LastName
    .


    Table CarService
    serviceID(PK)
    DescriptionOfFailure
.
.
Table ServiceDepartment
clientID(PK)
serviceID(PK)
price
.

реализация моего кода выглядит так:

@Entity
public class ServiceDepartmentBean implements ServiceDepartmentI {

    @EmbeddedId
    private ServiceDepartmentPK serviceDepartmentPK = new ServiceDepartmentPK();

    @Column(name="clientID", nullable=false, updatable=false, insertable=false)
    private  Long client;
    @Column(name="serviceId", nullable=false, updatable=false, insertable=false)
    private Long service;

    @Column
    private double price;
     .
    .
    .
    .
    public void setClient(ClientI client){
        serviceDepartmentPK.setClient(client);
    }
    public void setService(CarServiceI service){
        serviceDepartmentPK.setService(service);
    }
    public ClientI getClient(){
        return serviceDepartmentPK.getClient();
    }
    public CarServiceI getService(){
        return serviceDepartmentPK.getService();
    }
@Entity
public class CarServiceBean implements CarServiceI {

    @Id
    @GeneratedValue
    private long serviceId;

    @Column(nullable = false)
    private String descriptionOfFailure;

    @OneToMany(mappedBy="service")
private List<ServiceDepartmentI> servicesDepartments;

public List<ServiceDepartmentI> getServicesDepartments() {
    return servicesDepartments;
}
    .
    .
    //set i get method
@Entity
public class ClientBean implements ClientI {
        @Id
    @GeneratedValue
    private long clientID;

    @Column(nullable = false)
    private String firstName;

    @Column(nullable = false)
    private String lastName;

    @OneToMany(mappedBy = "client")
private List<ServiceDepartmentI> servicesDepartments;

public void setServicesDepartments(List<ServiceDepartmentI> servicesDepartments) {
    this.servicesDepartments = servicesDepartments;
}

public List<ServiceDepartmentI> getServicesDepartments() {
    return servicesDepartments;
}
}
public class ServiceDepartmentPK implements Serializable {

    @ManyToOne
    private ClientI client;

    @ManyToOne
    private CarServiceI service;

    public void setClient(ClientI client) {
        this.client = client;
    }

    public ClientI getClient() {
        return client;
    }

    public void setService(CarServiceI service) {
        this.service = service;
    }

    public CarServiceI getService() {
        return service;
    }
}

при попытке построить приложение выдает ошибку в консоли: StackTrace:

[INFO] annotations.EntityBinder Bind entity com.fit.beans.ServiceDepartmentBean on table ServiceDepartmentBean
[ERROR] ioc.Registry Error invoking constructor public org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(org.slf4j.Logger,java.util.List): java.lang.NullPointerException
[ERROR] ioc.Registry Operations trace:
[ERROR] ioc.Registry [ 1] Realizing service HibernateSessionSource
[ERROR] ioc.Registry [ 2] Instantiating service HibernateSessionSource implementation via org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(Logger, List) (at HibernateSessionSourceImpl.java:36) via org.apache.tapestry5.hibernate.HibernateCoreModule.bind(ServiceBinder) (at HibernateCoreModule.java:45)
[ERROR] ioc.Registry [ 3] Invoking constructor org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(Logger, List) (at HibernateSessionSourceImpl.java:36) via org.apache.tapestry5.hibernate.HibernateCoreModule.bind(ServiceBinder) (at HibernateCoreModule.java:45) (for service 'HibernateSessionSource')
[ERROR] HibernateCoreModule.HibernateSessionSource Construction of service HibernateSessionSource failed: Error invoking constructor public org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(org.slf4j.Logger,java.util.List): java.lang.NullPointerException
org.apache.tapestry5.ioc.internal.OperationException: Error invoking constructor public org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(org.slf4j.Logger,java.util.List): java.lang.NullPointerException
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.logAndRethrow(OperationTrackerImpl.java:121)
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:88)
    at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
    at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1121)
    at org.apache.tapestry5.ioc.internal.util.ConstructionPlan.createObject(ConstructionPlan.java:60)
    at org.apache.tapestry5.ioc.internal.ConstructorServiceCreator.createObject(ConstructorServiceCreator.java:61)
    at org.apache.tapestry5.ioc.internal.OperationTrackingObjectCreator$1.invoke(OperationTrackingObjectCreator.java:45)
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
    at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
    at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1121)

Любая помощь будет оценена =)

новая ошибка после рефакторинга трассировки стека:

[INFO] cfg.AnnotationBinder Binding entity from annotated class: com.fit.beans.CarServiceBean
[INFO] annotations.EntityBinder Bind entity com.fit.beans.CarServiceBean on table CarServiceBean
[INFO] cfg.AnnotationBinder Binding entity from annotated class: com.fit.beans.ServiceDepartmentBean
[INFO] annotations.EntityBinder Bind entity com.fit.beans.ServiceDepartmentBean on table ServiceDepartmentBean
[ERROR] ioc.Registry Error invoking constructor public org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(org.slf4j.Logger,java.util.List): No identifier specified for entity: com.fit.beans.ServiceDepartmentBean
[ERROR] ioc.Registry Operations trace:
[ERROR] ioc.Registry [ 1] Realizing service HibernateSessionSource
[ERROR] ioc.Registry [ 2] Instantiating service HibernateSessionSource implementation via org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(Logger, List) (at HibernateSessionSourceImpl.java:36) via org.apache.tapestry5.hibernate.HibernateCoreModule.bind(ServiceBinder) (at HibernateCoreModule.java:45)
[ERROR] ioc.Registry [ 3] Invoking constructor org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(Logger, List) (at HibernateSessionSourceImpl.java:36) via org.apache.tapestry5.hibernate.HibernateCoreModule.bind(ServiceBinder) (at HibernateCoreModule.java:45) (for service 'HibernateSessionSource')
[ERROR] HibernateCoreModule.HibernateSessionSource Construction of service HibernateSessionSource failed: Error invoking constructor public org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(org.slf4j.Logger,java.util.List): No identifier specified for entity: com.fit.beans.ServiceDepartmentBean
org.apache.tapestry5.ioc.internal.OperationException: Error invoking constructor public org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(org.slf4j.Logger,java.util.List): No identifier specified for entity: com.fit.beans.ServiceDepartmentBean
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.logAndRethrow(OperationTrackerImpl.java:121)
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:88)
    at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
    at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1121)
    at org.apache.tapestry5.ioc.internal.util.ConstructionPlan.createObject(ConstructionPlan.java:60)
    at org.apache.tapestry5.ioc.internal.ConstructorServiceCreator.createObject(ConstructorServiceCreator.java:61)
    at org.apache.tapestry5.ioc.internal.OperationTrackingObjectCreator$1.invoke(OperationTrackingObjectCreator.java:45)
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
    at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
    at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1121)
    at org.apache.tapestry5.ioc.internal.OperationTrackingObjectCreator.createObject(OperationTrackingObjectCreator.java:49)
    at org.apache.tapestry5.ioc.internal.SingletonServiceLifecycle.createService(SingletonServiceLifecycle.java:29)
    at org.apache.tapestry5.ioc.internal.LifecycleWrappedServiceCreator.createObject(LifecycleWrappedServiceCreator.java:46)
    at org.apache.tapestry5.ioc.internal.AdvisorStackBuilder.createObject(AdvisorStackBuilder.java:63)
    at org.apache.tapestry5.ioc.internal.InterceptorStackBuilder.createObject(InterceptorStackBuilder.java:54)
    at org.apache.tapestry5.ioc.internal.RecursiveServiceCreationCheckWrapper.createObject(RecursiveServiceCreationCheckWrapper.java:60)
    at org.apache.tapestry5.ioc.internal.OperationTrackingObjectCreator$1.invoke(OperationTrackingObjectCreator.java:45)
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
    at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
.
.
.
.
.
Caused by: java.lang.RuntimeException: Error invoking constructor public org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl(org.slf4j.Logger,java.util.List): No identifier specified for entity: com.fit.beans.ServiceDepartmentBean
    at org.apache.tapestry5.ioc.internal.util.ConstructorInvoker.invoke(ConstructorInvoker.java:57)
    at org.apache.tapestry5.ioc.internal.util.LoggingInvokableWrapper.invoke(LoggingInvokableWrapper.java:42)
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
    ... 72 more
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.fit.beans.ServiceDepartmentBean
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:672)
    at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at org.apache.tapestry5.internal.hibernate.HibernateSessionSourceImpl.<init>(HibernateSessionSourceImpl.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.apache.tapestry5.ioc.internal.util.ConstructorInvoker.invoke(ConstructorInvoker.java:48)
    ... 74 more

1 Ответ

3 голосов
/ 19 марта 2012

Удалить конструктор из

@EmbeddedId
private ServiceDepartmentPK serviceDepartmentPK;

Удалить аннотацию @ManyToOne из класса ServiceDepartmentPK и установить для типов полей значение long или int

@Embeddable

public class ServiceDepartmentPK  implements Serializable {

private static final long serialVersionUID = 1L;
@Column(name="COLUMN_NAME")
private long clientId;

@Column(name="COLUMN_NAME")
private long carServiceId;

public ServiceDepartmentPK  () {
}
// getters setters for client and carserver ids-s

public boolean equals(Object other) {
    if (this == other) {
        return true;
    }
    if (!(other instanceof ServiceDepartmentPK  )) {
        return false;
    }
    ServiceDepartmentPK  castOther = (ServiceDepartmentPK  )other;
    return 
        (this.scopeId == castOther.clientId)
        && (this.scriptId == castOther.carServiceId);

}

public int hashCode() {
    final int prime = 31;
    int hash = 17;
    hash = hash * prime + ((int) (this.clientId ^ (this.clientId>>> 32)));
    hash = hash * prime + ((int) (this.carServiceId ^ (this.carServiceId>>> 32)));

    return hash;
}

}

...