0

我想创建一个如下表:

    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

 }
4

2 回答 2

2

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

| 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。

于 2013-02-02T05:57:53.307 回答
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();
}

}

于 2013-02-07T21:37:36.870 回答