Hibernate - 从我的示例 tables 中获取错误的实体。看起来 table 列是 "exchanged"

Hibernate - get the wrong entities from my example tables. It looks like table columns are "exchanged"

我的 sql 服务器 2016 数据库中有三个 table 作为 Hibernate 应用程序的示例,该应用程序使用带有附加列的联接 table 来分配从客户那里购买的商品与特定数量。我在数据库中的 table 是:

table_article (a_id int, description varchar(255))

a_id   description  
1      fish         
2      fish         
3      water

table_customer (customer_id int, name varchar(255)

customer_id   name  
1      john         
2      jane         
3      jack 

ac_join_table (a_id, customer_id, amount, pk(a_id, customer_id))

a_id   customer_id amount  
1      2           10   
2      1           3        
2      3           7   
2      2           18        
3      1           5       

所以 id 为 2 的顾客 jane 买了 10 条鱼。 如果我 运行 我的程序看起来像连接 table 中的 table 列被解释不同或交换。 因为如果我 [=82= 】 下面代码jane买了3条鱼,18条鱼和7条鱼。看起来 article_id 被解释为 customer_id 如果我 运行:

// should return that jane (customer_id: 2) buys 10 fishes (a_id: 1, amount: 10)
// but it interprets a_id as customer_id so jane buys 3, 7 and 18 fishes...

Cust c = sessionObj.get(Cust.class, 2); 
List<ArtCustJoin> articles = c.getArtikel();
for (ArtCustJoin artCustJoin : articles) {
     System.out.println(artCustJoin.getArt().getDescription());
}

我的 Java classHibernate 模型是:

Art.java 对于 table_article

@Entity
@Table(name="table_article")
public class Art {
    @Id
    @Column(name = "a_id")
    private int article_id;

    @Column
    private String description;
     // …
    @OneToMany(mappedBy="ku")
    private List<ArtCustJoin> customers;     
    // …    
    // getters and setters
    // ...
}

Cust.java 对于 table_customer

@Entity
@Table(name="table_customer")
public class Cust {
    @Id
    private int customer_id;

    @Column
    private String name;

    @OneToMany(mappedBy="art")
    private List<ArtCustJoin> artikel;

    // getter + setter
    // ...
}

ArtCustJoin.java 用于加入 table ac_join_table。联接 table 有一个附加列 amount

@Entity
@Table(schema="dbo", name="ac_join_table")
public class ArtCustJoin {

    // Use Compound Key instead of single primitive key
    @EmbeddedId
    CompositeKey id;


    @ManyToOne
    @JoinColumn(name="a_id", columnDefinition="int", foreignKey = @ForeignKey(name = "FK_ARTID"))
    @MapsId("article_id") // maps to attribute with this name in class Artikel
    private Art art;

    @ManyToOne
    @JoinColumn(name="customer_id", foreignKey = @ForeignKey(name = "FK_CUSTID"))
    @MapsId("customer_id") // maps to attribute with this name in class Kunde
    private Cust ku;


    @Column
    private int amount;


    public CompositeKey getId() {
        return id;
    }

    public void setId(CompositeKey id) {
        this.id = id;
    }

    // getter + setter
    // ...
}

还有一个class作为组合键:

@Embeddable
public class CompositeKey implements Serializable{
    @Column
    private Integer a_id;

    @Column
    private Integer customer_id;

    public CompositeKey() {}
    // ...
}

我不知道错误在哪里,列 a_id 被解释为 customer_id...

我不是 java 开发人员,但 @OneToMany(mappedBy= 看起来都很奇怪,看起来像是问题的原因。 Customer 不应该被 customer_id 映射到 ArtCustJoin 吗?而不是 @OneToMany(mappedBy="art")。同样适用于Art class.