我有两个实体类 用户和WalletData 我想建立关系一个用户到许多带注释的钱包但我不知道如何。此外,我希望通过位于用户实体中的签名字段传递关系。
@Entity(name = "wallet_data")
public class WalletData implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "amount")
private Double amount;
@Column(name = "currency")
private String currency;
@JoinColumn(table = "user",referencedColumnName = "signature")
private User user; //Each wallet know who is his owner user
和
@Entity
@Table(name = "user")
@XmlRootElement
@NamedQueries(
{
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
@NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"),
@NamedQuery(name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.username = :username"),
@NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
@NamedQuery(name = "User.authorize", query = "SELECT u FROM User u WHERE u.password = :password and u.username = :username")
})
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;
@Basic(optional = false)
@Column(name = "username")
private String username;
@Basic(optional = false)
@Column(name = "password")
private String password;
@Column(name = "signature")
private String signature;
@JoinColumn(name = "user_id", referencedColumnName = "id")
@OneToOne(targetEntity = UserData.class)
private UserData userId;
@OneToMany(targetEntity = WalletData.class, cascade = CascadeType.ALL,mappedBy = "id")
private Set<WalletData> wallets; //The set contain all wallets of the current user
答案 0 :(得分:1)
您忘记了@ManyToOne
上的WalletData.user
注释。
并且mappedBy
属性应该是user
,因为那是构成关联另一侧的字段。
文档中描述了双向OneToMany关联。阅读它:http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#d5e5564