为什么targetEntity不是实体?

时间:2016-12-13 09:30:02

标签: java hibernate maven jpa jboss

我目前正在开发一个带有3个模块的Maven项目的应用程序。这三个模块是住宿,用户和反馈。在反馈表中,我有2个FK(一个来自用户,另一个来自住宿)。这是feedbackEntity类:

  @Entity
@Table(name = "feedback")
public class FeedbackEntity {
    private int feedbackId;
    private String reviewTitle;
    private String reviewText;
    private Date reviewDate;
    private UserEntity userEntity;
    private int overallRating;
    private AccommodationEntity accomodationEntity;
    private List<FeedbackDetailEntity> feedbackDetailEntity;

    public FeedbackEntity(int feedbackId, String reviewTitle, String reviewText, Date reviewDate,
            int overallRating) {
        super();
        this.feedbackId = feedbackId;
        this.reviewTitle = reviewTitle;
        this.reviewText = reviewText;
        this.reviewDate = reviewDate;
        this.overallRating = overallRating;
    }

    public FeedbackEntity(FeedbackPojo feedbackPojo) {
        this(feedbackPojo.getFeedbackId(), feedbackPojo.getReviewTitle(),
                feedbackPojo.getReviewText(), feedbackPojo.getReviewDate(),
                feedbackPojo.getOverallRating());
    }

    public FeedbackEntity() {

    }

    @ManyToOne(optional = false, targetEntity = AccommodationEntity.class)
    @JoinColumn(name = "accomodation_id")
    public AccommodationEntity getAccomodationEntity() {
        return accomodationEntity;
    }

    public void setAccomodationEntity(AccommodationEntity accomodationEntity) {
        this.accomodationEntity = accomodationEntity;
    }

    @ManyToOne(optional = false, targetEntity= UserEntity.class)
    @JoinColumn(name = "user_id")
    public UserEntity getUserEntity() {
        return userEntity;
    }

    public void setUserEntity(UserEntity userEntity) {
        this.userEntity = userEntity;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "feedback_id")
    public int getFeedbackId() {
        return feedbackId;
    }

    public void setFeedbackId(int feedbackId) {
        this.feedbackId = feedbackId;
    }

    @Column(name = "review_title")
    public String getReviewTitle() {
        return reviewTitle;
    }

    public void setReviewTitle(String reviewTitle) {
        this.reviewTitle = reviewTitle;
    }

    @Column(name = "review_text")
    public String getReviewText() {
        return reviewText;
    }

    public void setReviewText(String reviewText) {
        this.reviewText = reviewText;
    }

    @Column(name = "review_date")
    public Date getReviewDate() {
        return reviewDate;
    }

    public void setReviewDate(Date reviewDate) {
        this.reviewDate = reviewDate;
    }

    @Column(name = "overall_rating")
    public int getOverallRating() {
        return overallRating;
    }

    public void setOverallRating(int overallRating) {
        this.overallRating = overallRating;
    }

    @OneToMany(mappedBy="feedbackEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public List<FeedbackDetailEntity> getFeedbackDetail() {
        return feedbackDetailEntity;
    }

    public void setFeedbackDetail(List<FeedbackDetailEntity> feedbackDetailEntity) {
        this.feedbackDetailEntity = feedbackDetailEntity;
    }

    public FeedbackPojo toFeedbackPojo() {
        return new FeedbackPojo(feedbackId, reviewTitle, reviewText, reviewDate, overallRating);
    }

}

这是UserEntity类:

@Entity
@Table(name = "user")
public class UserEntity {
    private int userId;
    private String username;
    private String name;
    private String surname;
    private String email;

    public UserEntity(int user_id, String username, String name, String surname, String email) {
        super();
        this.userId = user_id;
        this.username = username;
        this.name = name;
        this.surname = surname;
        this.email = email;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

这是AccommodationEntity类:

    @Entity
@Table(name = "accommodation")
public class AccommodationEntity {
    private long accommodationId;
    private String name;
    private String type;
    private String description;
    private String country;
    private String city;
    private String adress;
    private byte[] image;
    private List<AccommodationDetailEntity> accommodationDetailEntity;

    public AccommodationEntity(long accomodation_id, String name, String type, String description,
            String country, String city, String adress, byte[] image) {
        super();
        this.accommodationId = accomodation_id;
        this.name = name;
        this.type = type;
        this.description = description;
        this.country = country;
        this.city = city;
        this.adress = adress;
        this.image = image;
    }

    public AccommodationEntity() {
    }

    public AccommodationEntity(AccommodationPojo accommodationPojo) {
        this(accommodationPojo.getAccommodationId(), accommodationPojo.getName(),
                accommodationPojo.getType(), accommodationPojo.getDescription(),
                accommodationPojo.getCountry(), accommodationPojo.getCity(),
                accommodationPojo.getAdress(), accommodationPojo.getImage());
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "accommodation_id")
    public long getAccommodationId() {
        return accommodationId;
    }

    public void setAccommodationId(long accommodationId) {
        this.accommodationId = accommodationId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    @Column(columnDefinition = "BLOB")
    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public AccommodationPojo toAccomodationPojo() {
        return new AccommodationPojo(accommodationId, name, type, description, country, city,
                adress, image);
    }

    @OneToMany(mappedBy = "accommodationEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public List<AccommodationDetailEntity> getAccommodationDetailEntity() {
        return accommodationDetailEntity;
    }

    public void setAccommodationDetailEntity(
            List<AccommodationDetailEntity> accommodationDetailEntity) {
        this.accommodationDetailEntity = accommodationDetailEntity;
    }

}

这是我在运行wildfly时遇到的错误:

10:14:05,245 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 59) MSC000001: Failed to start service jboss.persistenceunit."feedback-service-0.0.1-SNAPSHOT.war#jpa-app": org.jboss.msc.service.StartException in service jboss.persistenceunit."feedback-service-0.0.1-SNAPSHOT.war#jpa-app": java.lang.NoClassDefFoundError: com/comtrade/trips/service/user/entity/UserEntity
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:179)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:121)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:193)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.NoClassDefFoundError: com/comtrade/trips/service/user/entity/UserEntity
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.hibernate.annotations.common.reflection.java.JavaXClass.getDeclaredMethodProperties(JavaXClass.java:112)
    at org.hibernate.annotations.common.reflection.java.JavaXClass.getDeclaredProperties(JavaXClass.java:129)
    at org.hibernate.annotations.common.reflection.java.JavaXClass.getDeclaredProperties(JavaXClass.java:121)
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:251)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:717)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)
    at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:161)
    ... 7 more
Caused by: java.lang.ClassNotFoundException: com.comtrade.trips.service.user.entity.UserEntity from [Module "deployment.feedback-service-0.0.1-SNAPSHOT.war:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93)
    ... 23 more

10:14:05,750 INFO  [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 58) HHH000229: Running schema validator
10:14:05,750 INFO  [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 61) HHH000229: Running schema validator
10:14:07,940 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 68) RESTEASY002225: Deploying javax.ws.rs.core.Application: class com.comtrade.trips.service.accommodation.application.MyApplication
10:14:07,940 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 66) RESTEASY002225: Deploying javax.ws.rs.core.Application: class com.comtrade.trips.service.accommodation.application.AccommodationApplication
10:14:08,003 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 66) WFLYUT0021: Registered web context: /accommodation-service-0.0.1-SNAPSHOT
10:14:08,018 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 68) WFLYUT0021: Registered web context: /accommodation-service
10:14:08,034 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "feedback-service-0.0.1-SNAPSHOT.war")]) - failure description: {
    "WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"feedback-service-0.0.1-SNAPSHOT.war#jpa-app\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"feedback-service-0.0.1-SNAPSHOT.war#jpa-app\": java.lang.NoClassDefFoundError: com/comtrade/trips/service/user/entity/UserEntity
    Caused by: java.lang.NoClassDefFoundError: com/comtrade/trips/service/user/entity/UserEntity
    Caused by: java.lang.ClassNotFoundException: com.comtrade.trips.service.user.entity.UserEntity from [Module \"deployment.feedback-service-0.0.1-SNAPSHOT.war:main\" from Service Module Loader]"},
    "WFLYCTL0412: Required services that are not installed:" => ["jboss.persistenceunit.\"feedback-service-0.0.1-SNAPSHOT.war#jpa-app\""],
    "WFLYCTL0180: Services with missing/unavailable dependencies" => undefined
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

检查您的persistence.xml文件。在此文件中,您可能具有<persistence-unit>个列出的实体类。

选项1) 通过<class>package.YourEntity</class>

列出您未解决的实体

选项2) 或者通过添加<exclude-unlisted-classes>false</exclude-unlisted-classes>,无需像选项1中那样手动列出所有实体。