JPA Unknown mappedBy in:ServiceContract.serviceLocation,引用的属性unknown:ServiceLocation.service_contract

时间:2016-10-13 21:07:40

标签: java hibernate jpa

以下是我的ServiceContract.java

package com.cerebro.model.domain;

import java.io.Serializable;
import java.util.logging.Logger;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

import com.google.common.base.MoreObjects;

@Entity
@Table(name = "service_location")
public class ServiceLocation extends AbstractBaseEntity implements Identifiable<Long>, Serializable {
    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(ServiceLocation.class.getName());

    // Raw attributes
    private Long id;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;

    // Many to one
    private ServiceContract ServiceContract;

    @Override
    public String entityClassName() {
        return ServiceLocation.class.getSimpleName();
    }

    // -- [id] ------------------------

    @Override
    @Column(name = "id", precision = 19)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "service_location_id_seq")
    @SequenceGenerator(name = "service_location_id_seq", sequenceName = "service_location_id_seq", allocationSize = 1)
    @Id
    public Long getId() {
        return id;
    }

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

    public ServiceLocation id(Long id) {
        setId(id);
        return this;
    }

    @Override
    @Transient
    public boolean isIdSet() {
        return id != null;
    }
    // -- [address1] ------------------------

    @NotEmpty
    @Size(max = 500)
    @Column(name = "address1", nullable = false, length = 500)
    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public ServiceLocation address1(String address1) {
        setAddress1(address1);
        return this;
    }
    // -- [address2] ------------------------

    @Size(max = 500)
    @Column(name = "address2", length = 500)
    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public ServiceLocation address2(String address2) {
        setAddress2(address2);
        return this;
    }
    // -- [city] ------------------------

    @NotEmpty
    @Size(max = 50)
    @Column(name = "city", nullable = false, length = 50)
    public String getCity() {
        return city;
    }

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

    public ServiceLocation city(String city) {
        setCity(city);
        return this;
    }
    // -- [state] ------------------------

    @NotEmpty
    @Size(max = 100)
    @Column(name = "`state`", nullable = false, length = 100)
    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public ServiceLocation state(String state) {
        setState(state);
        return this;
    }
    // -- [zip] ------------------------

    @NotEmpty
    @Size(max = 50)
    @Column(name = "zip", nullable = false, length = 50)
    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public ServiceLocation zip(String zip) {
        setZip(zip);
        return this;
    }
    // -- [country] ------------------------

    @Size(max = 50)
    @Column(name = "country", length = 50)
    public String getCountry() {
        return country;
    }

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

    public ServiceLocation country(String country) {
        setCountry(country);
        return this;
    }
    // -- [createdts] ------------------------


    public ServiceLocation createdts(Long createdts) {
        setCreatedts(createdts);
        return this;
    }
    // -- [updatedts] ------------------------

    public ServiceLocation updatedts(Long updatedts) {
        setUpdatedts(updatedts);
        return this;
    }
    // -- [createdby] ------------------------

    public ServiceLocation createdby(String createdby) {
        setCreatedby(createdby);
        return this;
    }

    // -----------------------------------------------------------------
    // Many to One support
    // -----------------------------------------------------------------

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // many-to-one: ServiceLocation.serviceContract ==> ServiceContract.id
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//    @NotNull
//    @JoinColumn(name = "service_contract_id", nullable = false)
//    @ManyToOne
    @OneToOne
    @JoinColumn(name = "service_contract_id")
    public ServiceContract getServiceContract() {
        return ServiceContract;
    }

    /**
     * Set the {@link #serviceContract} without adding this ServiceLocation instance on the passed {@link #serviceContract}
     */
    public void setServiceContract(ServiceContract ServiceContract) {
        this.ServiceContract = ServiceContract;
    }

    public ServiceLocation serviceContract(ServiceContract ServiceContract) {
        setServiceContract(ServiceContract);
        return this;
    }

    /**
     * Apply the default values.
     */
    public ServiceLocation withDefaults() {
        return this;
    }

    /**
     * Equals implementation using a business key.
     */
    @Override
    public boolean equals(Object other) {
        return this == other || (other instanceof ServiceLocation && hashCode() == other.hashCode());
    }

    private IdentifiableHashBuilder identifiableHashBuilder = new IdentifiableHashBuilder();

    @Override
    public int hashCode() {
        return identifiableHashBuilder.hash(log, this);
    }

    /**
     * Construct a readable string representation for this ServiceLocation instance.
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this) //
                .add("id", getId()) //
                .add("address1", getAddress1()) //
                .add("address2", getAddress2()) //
                .add("city", getCity()) //
                .add("state", getState()) //
                .add("zip", getZip()) //
                .add("country", getCountry()) //
                .add("createdts", getCreatedts()) //
                .add("updatedts", getUpdatedts()) //
                .add("createdby", getCreatedby()) //
                .toString();
    }
}

和ServiceLocation.java

model

当我部署时,我不断受到以下问题:org.hibernate.AnnotationException:未知的mappedBy in:com.cerebro.model.domain.ServiceContract.serviceLocation,引用的属性unknown:com.cerebro.model.domain.ServiceLocation.service_contract 我错过了什么?

1 个答案:

答案 0 :(得分:0)

mappedBy属性名称应以小写's'开头:

@OneToOne(mappedBy = "serviceContract", cascade = CascadeType.PERSIST)
public ServiceLocation getServiceLocation() {
    return ServiceLocation;
}

即使字段ServiceLocation.ServiceContract的名称以大写'S'开头,您也要注释getter方法,而JPA规范指示属性名称将使用getter方法名称派生。 JavaBean约定(即从方法名称中删除'get'并将第一个字符转换为小写,除非后面跟着另一个大写字母)。