在超类上使用@Id时,获取“ org.hibernate.AnnotationException:未为子实体指定标识符:”

时间:2018-11-13 17:24:43

标签: spring hibernate

我有两个类,一个是实体的子类,另一个是包含@Id的超类;如下所示。

基本上,它无法创建LocalContainerEntityManagerFactoryBean(在底部的代码),因为它无法在超类上找到@Id。但是,当我从子类中删除超类扩展时,它可以工作。任何帮助表示赞赏,谢谢。

超级类:

@MappedSuperclass
public class GeneralCampaign {

    @Id
    protected String id;
    protected String campaignName;
    protected String accountId;
    protected String createdBy;
    protected STATUS status;

    public GeneralCampaign(){
        this.id = ("cus-"+ UUID.randomUUID());
        this.status = STATUS.DRAFT;
    }

    public GeneralCampaign(User userid,String accountId) {
        this.id = ("cus-"+ UUID.randomUUID());
        this.createdBy=userid.getId();
        this.status = STATUS.DRAFT;
        this.accountId=accountId;
        //this.isEditing = false;
    }

    public GeneralCampaign(String campaignName, String accountId, User user) {
        this.id = ("cus-"+ UUID.randomUUID());
        this.campaignName = campaignName;
        this.accountId = accountId;
        if (user != null)
            this.createdBy = user.getId();
    }

    // getter, setters
}

儿童班:

@Entity(name = "local_search_campaign")
public class LocalSearchCampaign extends GeneralCampaign {

    @Column(name = "start_date")
    private Date startDate;
    @Column(name = "end_date")
    private Date endDate;
    @Column(name = "time")
    private java.util.Date timeStamp;
    @Column(name = "campaign_guard_level")
    private Integer campaignGuardLevel = 4;

    @Column(name = "campaign_guard_enabled")
    private Boolean campaignGuardEnabled;

    @ElementCollection
    @CollectionTable(
            name = "local_search_campaign_keyword",
            joinColumns=@JoinColumn(name = "local_search_campaign_id", referencedColumnName = "id")
            )
    @Column(name="keyword")
    private List<String> keywords = new ArrayList<>();
    @OneToMany(
            mappedBy="localSearchCampaign",
            orphanRemoval = true,
            cascade = CascadeType.ALL,
            fetch=FetchType.LAZY
            )
    private List<Location> locations = new ArrayList<Location>();
    @Enumerated(EnumType.STRING)
    private STATUS status;
    @OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL)
    private LocalSearchPayment campaignPayment;
    @Column(name="landing_page_choice")
    private String landingPageChoice;
    @Column(name="landing_page_URI")
    private String landingPageURI;
    @Column(name="last_updated")
    private java.util.Date lastUpdated;
    @Column(name="is_editing",columnDefinition="default 0")
    private boolean editing = false;
    private String discountCode;

    public LocalSearchCampaign() {
        super();
        //this.id= ("LOC"+UUID.randomUUID().toString());
        this.timeStamp = new java.util.Date() ;
        //this.status=STATUS.DRAFT;

    }

    public LocalSearchCampaign(User userid,String accountId) {
        super(userid, accountId);
        /*this.id = ("cus-"+ UUID.randomUUID());
        this.createdBy=userid.getId();
        this.status = STATUS.DRAFT;
        this.accountId=accountId;*/
        //this.isEditing = false;
    }

    public LocalSearchCampaign(String campaignName, String accountId, User user) {
        super(campaignName, accountId, user);
        /*this.campaignName = campaignName;
        this.accountId = accountId;
        if (user != null)
            this.createdBy = user.getId();*/
    }

    // getter, setters
}

我正在创建一个LocalContainerEntityManagerFactoryBean,如下所示

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean userEntityManager() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(userDataSource());
    em.setPackagesToScan(new String[] { "com.packagename.etc"});

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put("hibernate.dialect",env.getProperty("spring.datasource.hibernate.dialect"));
    properties.put("hibernate.hbm2ddl.auto",env.getProperty("spring.datasource.hibernate.hbm2ddl.auto"));
    //properties.put("hibernate.format_sql", true);
    //properties.put("hibernate.show_sql", true);
    em.setJpaPropertyMap(properties);
    return em;
}

当我不使用上面的userEntityManager()方法而仅使用默认方法时,似乎可以找到超类的@Id

1 个答案:

答案 0 :(得分:0)

检查您的父类是否在com.packagename.etc包中。 似乎找不到它, 您也可以只设置:

#include <stdio.h>

int main()
{
    int number, x = 0, digit, temporary, div = 10, sum = 0;
    printf("Enter numbers\n");
    scanf("%d", &number);
    temporary = number;

START:

    digit = number%div;
    sum = sum*10+digit;
    number = number/div;

    if(number>0)
        goto START;

    printf("Reversed Number = %d\n", temporary);
    printf("Reversed Number = %d\n", sum);
    return 0;
}