Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

java - how to create a composite primary key hibernate JPA?

i try to create composite primary key in table from 2 foreign key using hibernate JPA,but gives me error.I find some solution on net but nothing

The relations between tables looks like this:

   Table Client

    clientID(PK)

    FirstName

    LastName
    .


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

implementation of my code looks like this:

@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;
    }
}

when try to build apllication throws me a error in console: 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)

Any help would be appreciated =)

new error after refactoring stacktrace:

[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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Remove constructor from

@EmbeddedId
private ServiceDepartmentPK serviceDepartmentPK;

Remove @ManyToOne annotation from ServiceDepartmentPK class and set fields types to long or 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;
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...