user1067665
user1067665

Reputation: 495

How can I add a single field from database to two different columns in a new table in the same entity with JPA

I want to create a table as below:

    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)

It means user A blocking uses B and C and .......

Is possible to do so:

@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

 }

Upvotes: 0

Views: 68

Answers (2)

user1067665
user1067665

Reputation: 495

It's solved

 @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();
}

}

Upvotes: 0

mcalex
mcalex

Reputation: 6778

You just need a one value in the 'Blockee' column, and to create a new row every time A Blocks someone. So:

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

So 5 has blocked 6, 7, 8, 10 and 9 has blocked 5.

Upvotes: 2

Related Questions