当通过JPQL检索对象时,Hibernate和JPA不会映射到子类

时间:2017-11-12 12:22:19

标签: spring hibernate jpa hibernate-mapping jpql

我有一个关于网页设计的大学项目。我们正在使用Hibernate,JPA和Srpring框架。 我为系统的actor(Actor.java)提供了一个抽象的超类,它与Message.java有关系。每当尝试执行以下JPQL查询时出现问题:“从消息m中选择m”,以检索存储在数据库(MySQL)中的消息。它应该返回消息,但它返回以下消息:

javax.persistence.EntityNotFoundException: Unable to find domain.Actor with id 3759

消息正确存储在数据库中,就我而言,Java应该将actor映射到它们的子类。并且具有该id的Actor(Administrator.java)的子类也正确存储

如何正确纠正此错误?

顺便说一下,这里是Actor和Message类(我的项目中的每个实体都扩展了DomainEntity,其中包含必要的id,version,...,属性)。

演员:

package domain;

import java.util.Collection;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;

import security.UserAccount;

@Entity
@Access(AccessType.PROPERTY)
public class Actor extends DomainEntity {

    //Atributos --------------------------
    private String                      name;
    private String                      surname;
    private String                      email;
    private PhoneNumber                 phone;
    private String                      address;
    private int                         suspicious;

    //Relaciones -------------------------
    private Collection<SocialIdentity>  socialIdentities;
    private Collection<Folder>          folders;

    private Collection<UserAccount>     userAccounts;


    @NotBlank
    public String getName() {
        return this.name;
    }
    public void setName(final String name) {
        this.name = name;
    }

    @NotBlank
    public String getSurname() {
        return this.surname;
    }
    public void setSurname(final String surname) {
        this.surname = surname;
    }

    @NotBlank
    @Email
    public String getEmail() {
        return this.email;
    }
    public void setEmail(final String email) {
        this.email = email;
    }

    @Valid
    public PhoneNumber getPhone() {
        return this.phone;
    }
    public void setPhone(final PhoneNumber phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return this.address;
    }
    public void setAddress(final String address) {
        this.address = address;
    }

    @NotNull
    @Range(min = 0, max = 1)
    public int getSuspicious() {
        return this.suspicious;
    }

    public void setSuspicious(final int suspicious) {
        this.suspicious = suspicious;
    }
    @OneToMany
    public Collection<SocialIdentity> getSocialIdentities() {
        return this.socialIdentities;
    }
    public void setSocialIdentities(final Collection<SocialIdentity> socialIdentities) {
        this.socialIdentities = socialIdentities;
    }

    @NotEmpty
    @OneToMany
    public Collection<Folder> getFolders() {
        return this.folders;
    }
    public void setFolders(final Collection<Folder> folders) {
        this.folders = folders;
    }

    @OneToMany(cascade = CascadeType.ALL)
    public Collection<UserAccount> getUserAccounts() {
        return this.userAccounts;
    }

    public void setUserAccounts(final Collection<UserAccount> userAccounts) {
        this.userAccounts = userAccounts;
    }

}

并留言:

package domain;

import java.util.Collection;
import java.util.Date;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.NotBlank;

@Entity
@Access(AccessType.PROPERTY)
public class Message extends DomainEntity {

    //Atributos -----------------------
    private Date                moment;
    private String              subject;
    private String              body;
    private String              priority;

    //Relaciones ----------------------
    private Actor               sender;
    private Actor               recipient;
    private Collection<Folder>  folders;


    @NotNull
    @Past
    @Temporal(TemporalType.TIMESTAMP)
    public Date getMoment() {
        return this.moment;
    }
    public void setMoment(final Date moment) {
        this.moment = moment;
    }

    @NotBlank
    public String getSubject() {
        return this.subject;
    }
    public void setSubject(final String subject) {
        this.subject = subject;
    }

    @NotBlank
    public String getBody() {
        return this.body;
    }
    public void setBody(final String body) {
        this.body = body;
    }

    @Pattern(regexp = "^HIGH|NEUTRAL|LOW$")
    public String getPriority() {
        return this.priority;
    }
    public void setPriority(final String priority) {
        this.priority = priority;
    }

    @NotNull
    @Valid
    @ManyToOne(optional = false)
    public Actor getSender() {
        return this.sender;
    }
    public void setSender(final Actor sender) {
        this.sender = sender;
    }

    @NotNull
    @Valid
    @ManyToOne(optional = false)
    public Actor getRecipient() {
        return this.recipient;
    }
    public void setRecipient(final Actor recipient) {
        this.recipient = recipient;
    }
    @ManyToMany
    public Collection<Folder> getFolders() {
        return this.folders;
    }
    public void setFolders(final Collection<Folder> folder) {
        this.folders = folder;
    }

}

这也是pom.xml的相关内容:

<build>
    <plugins>

        <!-- Java compiler -->

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <showWarnings>true</showWarnings>
                <compilerArguments>
                    <Xlint />
                </compilerArguments>
            </configuration>
        </plugin>

        <!-- Population -->

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>utilities.PopulateDatabase</mainClass>
            </configuration>
        </plugin>

        <!-- Tomcat 7 -->

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <server>tomcat-development-server</server>
                <port>8080</port>
                <path>/${project.artifactId}</path>
            </configuration>
        </plugin>

    </plugins>
</build>

<!-- Repositories to download components -->

<repositories>

    <!-- Spring repository -->

    <repository>
        <id>springsource</id>
        <name>SpringSource Repository</name>
        <url>http://repo.springsource.org/release</url>
    </repository>

    <!-- Apache repository -->

    <repository>
        <id>apache</id>
        <url>https://repository.apache.org/content/repositories/snapshots</url>
    </repository>

    <!-- Default repository -->

    <repository>
        <id>central</id>
        <url>http://repo.maven.apache.org/maven2</url>
    </repository>

</repositories>

<!--  Dependencies -->

<dependencies>

    <!-- Spring Framework -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.0.0.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>                                  
        </exclusion>
        </exclusions>
    </dependency>

    <!-- Spring Framework - Data -->

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.4.3.RELEASE</version>
    </dependency>

    <!-- Spring Framework - Security -->

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>3.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>3.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>3.2.0.RELEASE</version>
    </dependency>

    <!-- Spring Framework - Testing -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>

    <!-- Apache -->

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>2.2.2</version>
    </dependency>

    <!-- MySQL -->

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
    </dependency>

    <!-- Hibernate -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>4.2.3.Final</version>
    </dependency>

    <!-- Javax -->

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Display Tag -->

    <dependency>
        <groupId>displaytag</groupId>
        <artifactId>displaytag</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Standard taglibs -->

    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>           
        <version>1.1.2</version>
        <scope>provided</scope>
    </dependency>

    <!-- JUnit -->

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>

    <!-- Dependency patches -->

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.2</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.4</version>
    </dependency>

</dependencies>
P.S:我是这项技术的新手。

Administrator.java

package domain;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;

@Entity
@Access(AccessType.PROPERTY)
public class Administrator extends Actor {

}

DomainEntity.java

package domain;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Version;

@Entity
@Access(AccessType.PROPERTY)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class DomainEntity {

// Constructors -----------------------------------------------------------

public DomainEntity() {
    super();
}


// Identification ---------------------------------------------------------

private int id;
private int version;


@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public int getId() {
    return this.id;
}

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

@Version
public int getVersion() {
    return this.version;
}

public void setVersion(final int version) {
    this.version = version;
}

// Object interface -------------------------------------------------------

@Override
public int hashCode() {
    return this.getId();
}

@Override
public boolean equals(final Object other) {
    boolean result;

    if (this == other)
        result = true;
    else if (other == null)
        result = false;
    else if (other instanceof Integer)
        result = (this.getId() == (Integer) other);
    else if (!this.getClass().isInstance(other))
        result = false;
    else
        result = (this.getId() == ((DomainEntity) other).getId());

    return result;
}

@Override
public String toString() {
    StringBuilder result;

    result = new StringBuilder();
    result.append(this.getClass().getName());
    result.append("{");
    result.append("id=");
    result.append(this.getId());
    result.append(", version=");
    result.append(this.getVersion());
    result.append("}");

    return result.toString();
}

}

0 个答案:

没有答案