【问题标题】:Hibernate mapping in Java: org.hibernate.MappingException: Repeated column in mapping for entityJava中的休眠映射:org.hibernate.MappingException:实体映射中的重复列
【发布时间】:2013-12-06 12:12:48
【问题描述】:

我尝试收集两项服务的访问者统计信息。它包括每日访客统计和总体记录。每个服务都可以通过不同的名称访问。例如,用户、管理员、支持人员等。每个人都有自己的记录作为自己的统计数据。

这是我的数据库结构:

service_one:ID、名称

service_two:id、名称

daily_stats:id、日期、service_one_id、service_one_visitors、 service_two_id、service_two_visitors、overall_visitors

record_stats: id, service_one_id, service_one_record, service_one_record_date、service_two_id、service_two_record、 service_two_record_date

表之间的关系如下:

service_one ---(一对多)---> daily_stats(service_one_id)

service_one ---(一对多)---> record_stats(service_one_id)

service_two ---(一对多)---> daily_stats(service_two_id)

service_two ---(一对多)---> record_stats(service_two_id)

service_one 的映射(service_two 也是如此)。为了缩短示例,还省略了设置器:

@Entity
@Table(name = "service_one")
public class ServiceOne implements Serializable {
    private int id;
    private String name;

    private Set<RecordStats> recordStats = new HashSet<RecordStats>(0);
    private Set<DailyStats> dailyStats = new HashSet<DailyStats>(0);

    public ServiceOne() {}

    public ServiceOne(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    public int getId() {
        return id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    @OneToMany(fetch = LAZY, mappedBy = "service_one_id")
    public Set<RecordStats> getRecordStats() {
        return recordStats;
    }

    @OneToMany(fetch = LAZY, mappedBy = "service_one_id")
    public Set<DailyStats> getDailyStats() {
        return dailyStats;
    }
}

daily_stats映射:

@Entity
@Table(name = "daily_stats", uniqueConstraints = {
        @UniqueConstraint(columnNames = "date")
})
public class DailyStats implements Serializable{

    private int id;
    private Date date;
    private ServiceOne service_one_id;
    private int service_one_visitors;
    private ServiceTwo service_two_id;
    private int service_two_visitors;
    private int overall_visitors;

    public DailyStats() {}

    public DailyStats(DailyStats rec) {
        this.id = rec.getId();
        //...
    }

    @Id
    @GeneratedValue
    @Column(name = "id", nullable = false)
    public int getId() {
        return id;
    }

    @Temporal(DATE)
    @Column(name = "date")
    public Date getDate() {
        return date;
    }

    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "id", nullable = false)
    public ServiceOne getService_one_id() {
        return service_one_id;
    }

    @Column(name = "service_one_visitors")
    public int getService_one_visitors() {
        return service_one_visitors;
    }

    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "id", nullable = false)
    public ServiceTwo getService_two_id() {
        return service_two_id;
    }

    @Column(name = "service_two_visitors")
    public int getService_two_visitors() {
        return service_two_visitors;
    }

    @Column(name = "overall_visitors")
    public int getOverall_visitors() {
        return overall_visitors;
    }
}

record_stats映射:

@Entity
@Table(name = "record_stats", uniqueConstraints = {
        @UniqueConstraint(columnNames = "service_one_record_date"),
        @UniqueConstraint(columnNames = "service_two_record_date")
})
public class RecordStats implements Serializable {

    private int id;
    private ServiceOne service_one_id;
    private int service_one_record;
    private Date service_one_rec_date;
    private ServiceTwo service_two_id;
    private int service_two_record;
    private Date service_two_rec_date;

    public RecordStats() {}

    public RecordStats(RecordStats rec) {
        this.id = rec.getId();
        //...
    }

    @Id
    @GeneratedValue
    @Column(name = "id", nullable = false)
    public int getId() {
        return id;
    }

    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "id", nullable = false)
    public ServiceOne getService_one_id() {
        return service_one_id;
    }

    @Column(name = "service_one_record")
    public int getService_one_record() {
        return service_one_record;
    }

    @Column(name = "service_one_record_date")
    @Temporal(DATE)
    public Date getService_one_rec_date() {
        return service_one_rec_date;
    }

    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "id", nullable = false)
    public ServiceTwo getService_two_id() {
        return service_two_id;
    }

    @Column(name = "service_two_record")
    public int getService_two_record() {
        return service_two_record;
    }

    @Column(name = "service_two_record_date")
    @Temporal(DATE)
    public Date getService_two_rec_date() {
        return service_two_rec_date;
    }
}

尝试创建新条目抛出异常:

public static void main(String[] args) {
    ServiceOne serviceOne = new ServiceOne();
    serviceOne.setName("test");

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();

    session.beginTransaction();
    session.save(serviceOne);
    session.getTransaction().commit();

    //get records
    session = sessionFactory.openSession();
    session.beginTransaction();
    List result = session.createQuery("from service_one").list();
    for (ServiceOne o : (List<ServiceOne>)result) {
        System.out.println(o.getName());
    }
    session.getTransaction().commit();

    session.close();
}

org.hibernate.MappingException:实体映射中的重复列: VisitorCounter.model.entity.DailyStats 列:id(应该是 映射为 insert="false" update="false")

我的映射有什么问题?

【问题讨论】:

    标签: java hibernate hibernate-mapping h2


    【解决方案1】:

    在我看来

    @JoinColumn(name = "id", nullable = false)
    public ServiceOne getService_one_id() {
        return service_one_id;
    }
    

    在 DailyStats 中是错误的;你应该有name = "service_one_id"

    您在getService_two_id()RecordStats 中的同名方法中遇到了同样的问题。

    我还想问你为什么不在类字段中命名引用 serviceOneserviceTwo 而不是 service_one_idservice_two_id

    【讨论】:

    • 是的,你是对的。 ServiceTwo 也是如此。我是休眠和映射的新手。
    • 那就接受答案吧! :)
    • 我没有将引用命名为serviceOneserviceTwo,因为我想了解映射的工作原理。我更容易记住这些名字并“绘制”相应的图片。
    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2016-11-20
    • 2012-08-13
    • 2014-02-14
    相关资源
    最近更新 更多