使用SpringBoot和Vaadin的JAVA Javax嵌入实体,无法创建一个列

时间:2018-03-14 15:13:32

标签: jpa spring-boot entity javax

也许有人可以告诉我为什么我不能将Embeded Column注入我的Facility.class?我想在Facility.class表中从ContactPerson.class PrimaryKeys创建一个列,之后我想在Facility.class中创建contructor来添加ContactPerson。我是SpringBoot + Jpa和Vaadin ^^的新手 这是我收到的错误消息,我确定忽略了一些事情:

org.springframework.beans.factory.BeanCreationException:创建名称为' entityManagerFactory'的bean时出错在类路径资源中定义[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]:调用init方法失败;嵌套异常是org.hibernate.MappingException:找不到组件属性:id

    Caused by: org.hibernate.MappingException: component property not found: id
        at org.hibernate.mapping.Component.getProperty(Component.java:274) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]

Facility.class

 @Entity
    public class Facility extends BaseEntity {
        public Facility(@NotBlank String number, @NotBlank String name, @NotBlank String street, String extraInformation,
                        @NotBlank String zipcode, @NotBlank String city, **ContactPerson contactPerson**) {
            this.number = number;
            this.name = name;
            this.street = street;
            this.extraInformation = extraInformation;
            this.zipcode = zipcode;
            this.city = city;
            **//this.contactPerson = contactPerson;**
        }

        @NotBlank
        @Column(unique = true)
        private String number;
        @NotBlank
        private String name;
        @NotBlank
        private String street;
        private String extraInformation;
        @NotBlank
        private String zipcode;
        @NotBlank
        private String city;

        @Embedded
        @Column(columnDefinition = "contactperson")
        private ContactPerson contactPerson;

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }

        public String getName() {
            return name;
        }

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

        public String getStreet() {
            return street;
        }

        public void setStreet(String street) {
            this.street = street;
        }

        public String getExtraInformation() {
            return extraInformation;
        }

        public void setExtraInformation(String extraInformation) {
            this.extraInformation = extraInformation;
        }

        public String getZipcode() {
            return zipcode;
        }

        public void setZipcode(String zipcode) {
            this.zipcode = zipcode;
        }

        public String getCity() {
            return city;
        }

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

        public ContactPerson getContactPerson() {
            return contactPerson;
        }

        public void setContactPerson(ContactPerson contactPerson) {
            this.contactPerson = contactPerson;
        }
    }

ContactPerson.class

@Embeddable
public class ContactPerson extends BaseEntity {
    public ContactPerson(@NotBlank String titel, @NotBlank String firstName, @NotBlank String lastName, String telefon, String mobil, @Email String email) {
        this.titel = titel;
        this.firstName = firstName;
        this.lastName = lastName;
        this.telefon = telefon;
        this.mobil = mobil;
        this.email = email;
    }

    @NotBlank
    private String titel;
    @NotBlank
    private String firstName;
    @NotBlank
    private String lastName;
    private String telefon;
    private String mobil;
    @Email
    private String email;

    public String getTitel() {
        return titel;
    }

    public void setTitel(String titel) {
        this.titel = titel;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getTelefon() {
        return telefon;
    }

    public void setTelefon(String telefon) {
        this.telefon = telefon;
    }

    public String getMobil() {
        return mobil;
    }

    public void setMobil(String mobil) {
        this.mobil = mobil;
    }

    public String getEmail() {
        return email;
    }

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

BaseEntity.class

public abstract class BaseEntity extends AbstractAggregateRoot<BaseEntity> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    protected long getId() {
        return this.id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || this.getClass() != o.getClass())
            return false;
        BaseEntity that = (BaseEntity) o;
        return Objects.equals(this.id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.id);
    }

    @Override
    public String toString() {
        return "BaseEntity{" +
            "id=" + this.id +
            '}';
    }
}

1 个答案:

答案 0 :(得分:0)

经过长时间的尝试,我发现了问题所在。 Embeding之后的ContactPerson.class不再是实体,所以它不需要BaseEntity.class的扩展,但它需要默认的构造函数来启动。 Java9中不需要注释@Embedded

ContactPerson.class

@Embeddable
public class ContactPerson {
    public ContactPerson(@NotBlank String titel, @NotBlank String firstName, @NotBlank String lastName, String telefon, String mobil, @Email String email) {
        this.titel = titel;
        this.firstName = firstName;
        this.lastName = lastName;
        this.telefon = telefon;
        this.mobil = mobil;
        this.email = email;
    }

    public ContactPerson() {
    }

    @NotBlank
    private String titel;
    @NotBlank
    private String firstName;
    @NotBlank
    private String lastName;
    private String telefon;
    private String mobil;
    @Email
    private String email;

    public String getTitel() {
        return titel;
    }

    public void setTitel(String titel) {
        this.titel = titel;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getTelefon() {
        return telefon;
    }

    public void setTelefon(String telefon) {
        this.telefon = telefon;
    }

    public String getMobil() {
        return mobil;
    }

    public void setMobil(String mobil) {
        this.mobil = mobil;
    }

    public String getEmail() {
        return email;
    }

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

Facility.class

@Entity
public class Facility extends BaseEntity {
    public Facility(@NotBlank String number, @NotBlank String name, @NotBlank String street, String extraInformation, @NotBlank String zipcode, @NotBlank String city, ContactPerson contactPerson) {
        this.number = number;
        this.name = name;
        this.street = street;
        this.extraInformation = extraInformation;
        this.zipcode = zipcode;
        this.city = city;
        this.contactPerson = contactPerson;
    }

    public Facility() {
    }

    @NotBlank
    @Column(unique = true)
    private String number;
    @NotBlank
    private String name;
    @NotBlank
    private String street;
    private String extraInformation;
    @NotBlank
    private String zipcode;
    @NotBlank
    private String city;


    private ContactPerson contactPerson;

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getExtraInformation() {
        return extraInformation;
    }

    public void setExtraInformation(String extraInformation) {
        this.extraInformation = extraInformation;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public String getCity() {
        return city;
    }

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

    public ContactPerson getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(ContactPerson contactPerson) {
        this.contactPerson = contactPerson;
    }
}