java Hibernate正在查找表中不存在的DTYPE列

dy1byipe  于 2023-05-27  发布在  Java
关注(0)|答案(3)|浏览(196)

由于某种原因,Hibernate正在查找表中不存在的DTYPE列。我想知道为什么?在我以前的项目中,一切都很好。但在这里我添加了类自由职业者的层次结构,似乎不知何故,它导致的问题

Hibernate: 
    select
        user0_.id as id2_6_0_,
        user0_.email as email3_6_0_,
        user0_.first_name as first_na4_6_0_,
        user0_.last_name as last_nam5_6_0_,
        user0_.login as login6_6_0_,
        user0_.password as password7_6_0_,
        user0_.DTYPE as DTYPE1_6_0_,
        roles1_.user_id as user_id1_5_1_,
        roles1_.role as role2_5_1_ 
    from
        users user0_ 
    left outer join
        user_roles roles1_ 
            on user0_.id=roles1_.user_id 
    where
        user0_.id=?
WARN  SqlExceptionHelper - SQL Error: 0, SQLState: 42703
ERROR SqlExceptionHelper - ERROR: column user0_.dtype does not exist
  Position: 197

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

层次结构。Map的超类:

@MappedSuperclass
@Access(AccessType.FIELD)
public class BaseEntity implements Persistable<Integer> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    @Access(value = AccessType.PROPERTY)
    protected Integer id;

    public BaseEntity() {
    }

//getters and setters
}

实体用户:

@Entity
@Table(name = "users")
public class User extends BaseEntity {

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

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

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

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

    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
    @Column(name = "role")
    @ElementCollection(fetch = FetchType.EAGER)
    protected Set<Role> roles;

    public User(){}

//getters and setters
}

我有实体自由职业者

@Entity
public class Freelancer extends User {

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "freelancer_skills",
            joinColumns = {@JoinColumn(name = "freelancer_id")},
            inverseJoinColumns = {@JoinColumn(name = "skill_id")}
    )
    private Set<Skill> skills;

    public Freelancer(){}

//getters and setters
}

upd:角色类

import org.springframework.security.core.GrantedAuthority;

public enum Role implements GrantedAuthority {
    USER,
    ADMIN,
    FREELANCER,
    CLIENT;

    @Override
    public String getAuthority() {
        return name();
    }
}
tgabmvqs

tgabmvqs1#

@Enumerated(EnumType.STRING)
@CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "role")
@ElementCollection(fetch = FetchType.EAGER)

这里看起来很奇怪

@Column(name = "role")

因为您正在加入集合并尝试Map到列?
编辑:我没有仔细阅读。您应该使用某种类型的hibernate继承。读取http://www.thoughts-on-java.org/complete-guide-inheritance-strategies-jpa-hibernate/。我觉得这可能是个问题。Freelancer是否有自己的独立表?

lskq00tm

lskq00tm2#

我知道我应该像这样拆分Hibernate的类层次结构:

public class BaseUser extends BaseEntity {

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

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

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

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

    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
    @Column(name = "role")
    @ElementCollection(fetch = FetchType.EAGER)
    protected Set<Role> roles;

    public BaseUser(){}
}

则我应扩展BaseUser以创建实体User:

@Entity
@Table(name = "users")
public class User extends BaseUser {

public User(){}
}

创建实体Freelancer:

@Entity
public class Freelancer extends BaseUser {
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "freelancer_skills",
            joinColumns = {@JoinColumn(name = "freelancer_id")},
            inverseJoinColumns = {@JoinColumn(name = "skill_id")}
    )
    private Set<Skill> skills;

    public Freelancer(){}
}

所以它现在工作正常。

vdzxcuhz

vdzxcuhz3#

这是因为hibernate希望指定模型类型。在本例中,您有2个模型和1个DB表。
你可以(postgres示例):

alter table users add dtype varchar(31) not null default 'Freelancer';
alter table users alter column dtype drop default ;

然后您可以构建和部署。

相关问题