4

私はこのER図を持っています:

ここに画像の説明を入力

それはこれらのクラスに翻訳されました:

ユーザー.java

@Entity
@Table(name = "user")
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")})
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Collection<UserHasNotify> userHasNotifyCollection;

UserHasNotify.java

@Entity
@Table(name = "user_has_notify")
@NamedQueries({
    @NamedQuery(name = "UserHasNotify.findAll", query = "SELECT u FROM UserHasNotify u")})
public class UserHasNotify implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected UserHasNotifyPK userHasNotifyPK;
    @Column(name = "has_read")
    private String hasRead;
    @JoinColumn(name = "notify_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Notify notify;
    @JoinColumn(name = "user_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private User user;

UserHasNotifyPK.java

@Embeddable
public class UserHasNotifyPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "user_id")
    private int userId;
    @Basic(optional = false)
    @Column(name = "notify_id")
    private int notifyId;

Notify.java

@Entity
@Table(name = "notify")
@NamedQueries({
    @NamedQuery(name = "Notify.findAll", query = "SELECT n FROM Notify n")})
public class Notify implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "message")
    private String message;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "notify")
    private Collection<UserHasNotify> userHasNotifyCollection;

ここで、エンティティ User と Notify を追加し、それらの間の関係を作成します。だから私はこのスニペットを書きました:

        User user = new User();
        user.setName("John");

        Notify notify = new Notify();
        notify.setMessage("Hello World");

        userFacade.create(user);
        notifyFacade.create(notify);

        UserHasNotify uhn = new UserHasNotify();
        uhn.setNotify(notify);
        uhn.setUser(user); 
        uhn.setHasRead("ok");
        uhnFacade.create(uhn);

しかし、私はこのエラーを受け取ります:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'user_id' cannot be null
Error Code: 1048
Call: INSERT INTO user_has_notify (has_read, user_id, notify_id) VALUES (?, ?, ?)
    bind => [3 parameters bound]
Query: InsertObjectQuery(com.test.entity.UserHasNotify[ userHasNotifyPK=null ])

どうして???????????

4

1 に答える 1

0

エラーの原因は、リモート通信にある可能性があります。ファサードを使用しているという事実は、バックエンドとリモートで通信していることを意味します。

これは、インスタンスに設定しているuserおよびインスタンスが送信されてリモート システムに保持されることを意味しますが、ローカル インスタンスは生成された ID を受け取りません。notifyuhn

これを確認して修正するには、例を拡張できます。

userとを保存notifyしたら、バックエンドから取得します。これにより、既存の ID を持つ永続化されたインスタンスが返されます。次に、それらを使用して関係を保存できますuhn

編集UserHasNotify埋め込みIDとのステートフルな関係であるという事実を見逃しました。コードでは、この ID を設定していないため、プロバイダーはそれを見逃しています。

この場合、embededd Id の代わりに を使用することをお勧めします - マッピングはより読みやすいので、 andリレーションを 2 回IdClassマッピングすることはおそらくないでしょう- 1 回は埋め込み PK で、もう 1 回はエンティティで ;)UserNotify

これは次のようになります。

public class UserHasNotifyPK implements Serializable {
    private Notify notify;
    private User user;
    ...
    }

.

@Entity
@Table(name = "user_has_notify")
@IdClass(UserHasNotifyPK.class)
@NamedQueries({
    @NamedQuery(name = "UserHasNotify.findAll", query = "SELECT u FROM UserHasNotify u")})
public class UserHasNotify implements Serializable {
    
    private static final long serialVersionUID = 1L;
        
    @Column(name = "has_read")
    private String hasRead;
    
    @Id
    @JoinColumn(name = "notify_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Notify notify;
    
    @Id
    @JoinColumn(name = "user_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private User user;

その後、同じテストを再試行できます。

于 2013-02-07T14:36:09.920 に答える