单向OneToMany映射问题

时间:2014-06-12 14:41:20

标签: jpa jpa-2.0

我遇到了单向onetomany映射的一些问题。 我得到表用户和Rubrica:

User (
scode double precision NOT NULL,
...
CONSTRAINT utenti_pkey PRIMARY KEY (scode)
)

Rubrica (
id serial NOT NULL,
id_owner integer NOT NULL,
id_contact integer NOT NULL,
CONSTRAINT rubrica_pkey PRIMARY KEY (id ),
CONSTRAINT rubrica_fk01 FOREIGN KEY (id_owner)
  REFERENCES users (scode) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT rubrica_fk02 FOREIGN KEY (id_contact)
  REFERENCES users (scode) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION
)

不要介意用户双倍PKey。它是客户的表格,我无法对其进行修改。 Rubrica存储其所有者,用户和联系人之间的关系,也是一组用户。 用户映射如下:

@SuppressWarnings("serial")
@Entity
@Table(name = "utenti", schema = "public")
public class User implements Serializable {
  @Id
  @Column(name = "scode", unique = true, nullable = false)
  private Integer scode;
...
}

确定。问题来了。如果我像这样映射Rubrica:

public class Rubrica2 implements Serializable {
  @Id
  @Column(name = "id", nullable = false, unique = true)
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "id_owner", nullable = false, unique = true, referencedColumnName = "scode")
  private User owner;

  @OneToMany(fetch = FetchType.EAGER)
  @JoinColumn(name = "id_contact", nullable = false, updatable = false, insertable = true, referencedColumnName = "scode")
  private Set<User> relations = new HashSet<User>();
  ...
}

JBoss在部署时给了我这个例外:

Caused by: org.hibernate.MappingException: Unable to find column with logical name: scode in org.hibernate.mapping.Table(public.rubrica) and its related

supertables和secondary tables

如果我用这种方式映射Rubrica:

public class Rubrica2 implements Serializable {
  @Id
  @Column(name = "id", nullable = false, unique = true)
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "id_owner", nullable = false, unique = true, referencedColumnName = "scode")
  private User owner;

  @OneToMany(fetch = FetchType.EAGER, mappedBy = "scode")
  private Set<User> relations = new HashSet<User>();
  ...
}

我在运行时遇到了不好的行为。如果我运行此代码

r = new Rubrica2();
q2.setParameter("id", ownerID);
User owner = (User) q2.getSingleResult();
r.setOwner(owner);
q2.setParameter("id", contactID);
User u = (User) q2.getSingleResult();
r.getRelations().add(u);
entityManager.persist(r);

我遇到了这个例外:

Hibernate: insert into public.rubrica (id_owner) values (?)
11:08:19,440 DEBUG [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 7) 
  ERROR: null values in column "id_contact" violates not-null constraint [n/a]: org.postgresql.util.PSQLException: 
    ERROR: null values in column "id_contact" violates not-null constraint

我遵循理论指出here关于onetomany单向。 我使用的是JPA2.0,Hibernate4(由JBoss7.1.1.Final提供)和PostgresSQL。

1 个答案:

答案 0 :(得分:0)

这种映射或数据库设计毫无意义。如果您希望一个Rubrica拥有多个联系人,则您无法在rubrica表中拥有该关系的外键。外键只能引用一个联系人,而不是很多。

要映射这样的一对多关联,您需要用户使用rubrica中的外键(具有相同rubrica_id的所有用户都是此rubrica的联系人),或两个表之间的连接表。 / p>