Hibernate继承存在问题:@SecondaryTable注释

时间:2019-03-06 00:33:00

标签: java spring hibernate

我有以下课程:

//演员班

@Entity
@Table(name=Actor.TABLE_NAME)  
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public abstract class Actor{

    public static final String TABLE_NAME = "actor";

    @Id
    @Column(name="id")
    private String id = IdGenerator.createId();   

    public Actor() { }

    public String getId() { return id;  }
}

//公司类别

@Entity
@SecondaryTable(name = Company.TABLE_NAME, pkJoinColumns= {@PrimaryKeyJoinColumn(name=Company.PK_NAME)})
@DiscriminatorValue("company")

public class Company extends Actor {

    public static final String TABLE_NAME = "company";
    public static final String PK_NAME = "id";

    // Inherits Id from superclass Actor

    @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
    @JoinColumn(name="categoryid") 
    private Category category;

    @Column(name = "name", nullable=false)
    @Size(min=2, max=60, message="LATER")
    private String name;

    @Column(name="domainName", nullable=true)
    private String domainName;  

    @Column(name="registrationNumber", nullable=true)
    private String registrationNumber;

    @Column(name="description")
    private String description;


    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="itemId")
    private Set<Image> logos = new HashSet<>();

    @OneToMany(mappedBy="company", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    private Set<Branch> branches = new HashSet<>();

    public Company() {}

    public void addBranch(Branch branch) {
        branch.setCompany(this);
        branches.add(branch);
    }

    public void addLogo(Image logo) {

    }

// GETTERS AND SETTERS .......

}

//分支班级

@Entity
@Table(name=Branch.TABLE_NAME)
public class Branch {

    public static final String TABLE_NAME = "branch";

    @Id
    @Column(name = "id")
    private String id = IdGenerator.createId();

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="address")
    private Address address;

    @Column(name = "telephone")
    private String telephone;

    @ManyToOne
    @JoinColumn(name="company")
    private Company company;

    public Branch() {
        this.address.setAddressCategory(AddressCategory.BRANCH); 
    }

// GETTERS AND SETTERS

}

//地址类别

@Entity  
@Table(name=Address.TABLE_ADDRESS) 
public class Address {

    public static final String TABLE_ADDRESS = "address";
    @Id
    @Column(name="id")
    private String id = IdGenerator.createId();   

    @Column(name="category", nullable=false)
    private AddressCategory addressCategory;;

    @Column(name="streetname", nullable=true)
    private String streetName;

    @Column(name="number", nullable=true)
    private String number;

    @Column(name="postcode", nullable=true)
    private String postcode;

    @Column(name="city", nullable=true)
    private String city;

    @Column(name="country", nullable=true)
    private String country;

    @Column(name = "telephone")
    private String telephone;

    @Embedded
    private EmailAddress email;

    @Column(name="description", nullable=true)
    private String description;

    @ManyToOne
    @JoinColumn(name="actor")
    @NotNull(groups = {Update.class}) 
    @JsonIgnore
    private Actor actor;

    public Address() { }

    public Address(Actor actor) {
        this.actor = actor;
    }

// GETTERS AND SETTERS

}

当我使用邮递员的以下json运行以上代码时:

{
    "categoryId": "ee6e75c9-2d1b-41c1-8f12-236fbf907683",
    "name": "McDonalds",
    "domainName": "www.McDonalds.be",
    "registrationNumber": "5555555",
    "description": "Food and Drink"
}

我收到以下异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Field 'company0_.categoryid' not found in field list
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_151]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_151]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_151]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_151]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.44.jar:5.1.44]
    at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.44.jar:5.1.44]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943) ~[mysql-connector-java-5.1.44.jar:5.1.44
...
...

请注意,为了使文章简洁,我省略了控制器代码。上面的异常讨论了公司类中的 categoryid 属性。我删除了categorid,并用 description 属性

替换了categoryid

我认为问题在于使用辅助表进行注释。

这些表格如下:

CREATE TABLE actor
(
   id    VARCHAR(255)   DEFAULT '' NOT NULL,
   type  VARCHAR(255),
   PRIMARY KEY (id)
)


CREATE TABLE company
(
   id                    VARCHAR(255)   NOT NULL,
   categoryid            VARCHAR(255),
   name                  VARCHAR(255)   NOT NULL,
   `domainName`          VARCHAR(255)   NOT NULL,
   `registrationNumber`  VARCHAR(255),
   description           VARCHAR(255)   NOT NULL,
   PRIMARY KEY (id)
)

CREATE TABLE branch
(
   id         VARCHAR(255)   NOT NULL,
   company    VARCHAR(255),
   address    VARCHAR(255),
   telephone  VARCHAR(255),
   PRIMARY KEY (id)
)

CREATE TABLE address
(
   id           VARCHAR(255)   NOT NULL,
   category     VARCHAR(255),
   actor        VARCHAR(255),
   streetname   VARCHAR(255),
   number       VARCHAR(255),
   postcode     VARCHAR(255),
   city         VARCHAR(255),
   country      VARCHAR(255),
   telephone    VARCHAR(255),
   email        VARCHAR(255),
   description  VARCHAR(255),
   PRIMARY KEY (id)
)

1 个答案:

答案 0 :(得分:0)

经过研究,我自己找到了答案。由于我正在@Secondarytable类上使用Company注释,并且它也继承自Actor,因此我必须指定Company的属性应保留在哪个表上。通过为@Column的属性上的每个Company注释添加“ table = TABLE_NAME” ,可以解决此问题。如果@Column中没有“ table = TABLE_NAME” ,则Hibernate将不知道是否将Company属性保存在 actor 表或 company < / strong>表。