如何将数据库中的单个字段添加到使用JPA的同一实体的新表中的两个不同列

时间:2013-02-02 05:51:06

标签: jpa collections

我想创建一个表格如下:

    BLOCK_USER
    ************
    ID 
    USER_ID, (ID for a single user how want to block other users below)
    USER_ID, (A collection of IDS of users how are going to bee blocked by user above)

这意味着用户A阻止使用B和C以及.......

可以这样做:

@Entity
@Table(name = "BLOCK_USERS")
public class BlockUsers
{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private int id;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="USER_ID")
private User wantToStop_id;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="USER_ID")
private Collection<User> thoseUsers_ids;

   //getters and setters

 }

2 个答案:

答案 0 :(得分:2)

您只需在“Blockee”列中输入一个值,并在每次阻止某人时创建一个新行。所以:

| ID  |  BlockerID   |   BlockedID  |
|  1  |     5        |       6      |
|  2  |     5        |       7      |
|  3  |     5        |       8      |
|  4  |     5        |      10      |
|  5  |     9        |       5      |

所以5阻挡了6,7,8,10和9阻挡了5。

答案 1 :(得分:0)

它已经解决了

 @Entity
 @Table(name="BLOCKEE")
 @XmlRootElement
 @XmlAccessorType(XmlAccessType.FIELD) 
 public class Blockee
 {
@Id
@Column(name = "BLOCKEE_ID", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO)
@XmlElement
private int blockee_id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "BLOCKER_ID")
@XmlElement
private User blocker_id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "BLOCKED_ID")
@XmlElement
private User blocked_id;

// Constructors

/** default constructor */
public Blockee() {
}

/** minimal constructor */
public Blockee(int id) {
    this.blockee_id = id;
}

/** full constructor */
public Blockee(int id, User blockee, User userByBlockerId) {
    this.blockee_id = id;
    this.blocker_id = blockee;
    this.blocked_id = userByBlockerId;
}

public int getBlockee_id()
{
    return blockee_id;
}

public void setBlockee_id(int blockee_id)
{
    this.blockee_id = blockee_id;
}


public User getBlocker_id()
{
    return blocker_id;
}

public void setBlocker_id(User blocker_id)
{
    this.blocker_id = blocker_id;
}

public User getBlocked_id()
{
    return blocked_id;
}

public void setBlocked_id(User blocked_id)
{
    this.blocked_id = blocked_id;
}

@Override
public int hashCode()
{
    HashCodeBuilder builder = new HashCodeBuilder();
    builder.append(getBlockee_id());
    builder.append(getBlocked_id());
    builder.append(getBlocker_id());
    return builder.toHashCode();
}

@Override
public boolean equals(Object obj)
{
    if (obj instanceof User)
    {
        Blockee other = (Blockee) obj;
        EqualsBuilder builder = new EqualsBuilder();
        builder.append(getBlockee_id(),other.blockee_id);
        builder.append(getBlocker_id(),other.blocker_id);
        builder.append(getBlocked_id(),other.blocked_id);

        return builder.isEquals();
    }
    return false;
}

@Override
public String toString()
{
    return new ToStringCreator(this)
    .append("BLOCKEE_ID ", this.getBlockee_id())
    .append("BLOCKER_ID", this.getBlocker_id())
    .append("BLOCKED_ID ", this.getBlocked_id())

            .toString();
}

}