Java中的Hibernate映射:org.hibernate.MappingException:实体映射中的重复列

时间:2013-12-06 12:12:49

标签: java hibernate hibernate-mapping h2

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

这是我的数据库结构:

  

service_one:id,name

     

service_two:id,name

     

daily_stats:id,date,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)。为了缩短示例,省略了setter:

@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:实体映射中的重复列:   VisitorsCounter.model.entity.DailyStats列:id(应该是   map with insert =“false”update =“false”)

我的地图有什么问题?

1 个答案:

答案 0 :(得分: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