我有三个实体,交易员,投资组合和会员。每个交易者都有一个投资组合,投资组合可以有很多成员。我建立了以下关系。我不确定如何使用创建的Jointable,即Portfolio_PORTFOLIOID和members_MEMBERID。显然,我想将每个有问题的人与会员ID相关联,但我不知道如何解决这个问题。连接数据如何保持不变?
我的Portfolio
班级
@Entity
@Table(name="Portfolio")
@NamedQuery(
name="findPortfolioByTrader",
query="SELECT p FROM Portfolio p" +
" WHERE Trader = :trader"
)
public class Portfolio {
@Id
@GeneratedValue
private Integer portfolioId;
@Temporal(TIMESTAMP)
private Date lastUpdate;
private Integer balance;
private Trader trader;
private Collection<Member> members;
public Portfolio() {
this.lastUpdate = new Date();
}
public Portfolio(Integer balance, Trader trader) {
this.lastUpdate = new Date();
this.balance = balance;
this.trader = trader;
}
public Integer getPortfolioId() {
return portfolioId;
}
public void setPortfolioId(Integer portfolioId) {
this.portfolioId = portfolioId;
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
@ManyToMany
@JoinTable(
name="MEMBER_PORTFOLIO",
joinColumns=
@JoinColumn(name="Member_MEMBERID", referencedColumnName="MEMBERID"),
inverseJoinColumns=
@JoinColumn(name="portfolio_PORTFOLIOID", referencedColumnName="PORTFOLIOID")
)
public Collection<Member> getMembers() {
return members;
}
public void setMembers(Collection<Member> members) {
this.members = members;
}
@OneToOne(cascade=ALL, mappedBy="portfolio")
public Trader getTrader()
{
return trader;
}
public void setTrader(Trader trader)
{
this.trader = trader;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
}
我的Member
班级
@Entity
@Table(name="Member")
@NamedQuery(
name="findAllMembers",
query="SELECT m FROM Member m " +
"ORDER BY m.memberId"
)
public class Member implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -468520665316481235L;
private String memberId;
private String forename;
private String surname;
private Integer position;
private Integer majority;
private Integer IPO;
private Integer questions;
private Integer answers;
private Party party;
private Date lastUpdate;
private char status;
private Collection<Portfolio> portfolios;
private Collection<AskOrder> askOrders;
private Collection<BidOrder> bidOrders;
public Member() {
this.lastUpdate = new Date();
}
public Member(String memberId,String forename, String surname, Integer position,
Integer majority, Integer IPO, Integer questions, Integer answers, Party party) {
this.memberId = memberId;
this.forename = forename;
this.surname = surname;
this.position = position;
this.majority = majority;
this.IPO = IPO;
this.questions = questions;
this.answers = answers;
this.party = party;
this.lastUpdate = new Date();
this.askOrders = new ArrayList<AskOrder>();
this.bidOrders = new ArrayList<BidOrder>();
this.portfolios = new ArrayList<Portfolio>();
}
@Id
public String getMemberId() {
return memberId;
}
public void setMemberId(String memberId) {
this.memberId = memberId;
}
public char getStatus() {
return status;
}
public void setStatus(char status) {
this.status = status;
}
@Temporal(TIMESTAMP)
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
public String getForename()
{
return forename;
}
public void setForename(String forename)
{
this.forename = forename;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
public Integer getMajority() {
return majority;
}
public void setMajority(Integer majority) {
this.majority = majority;
}
public Integer getIPO() {
return IPO;
}
public void setIPO(Integer iPO) {
IPO = iPO;
}
public Integer getQuestions() {
return questions;
}
public void setQuestions(Integer questions) {
this.questions = questions;
}
public Integer getAnswers() {
return answers;
}
public void setAnswers(Integer answers) {
this.answers = answers;
}
@ManyToOne
public Party getParty() {
return party;
}
public void setParty(Party party) {
this.party = party;
}
@OneToMany(cascade=ALL, mappedBy="member")
public Collection<AskOrder> getAskOrders()
{
return askOrders;
}
public void setAskOrders(Collection<AskOrder> orders)
{
this.askOrders = orders;
}
@OneToMany(cascade=ALL, mappedBy="member")
public Collection<BidOrder> getBidOrders()
{
return bidOrders;
}
public void setBidOrders(Collection<BidOrder> bidOrders)
{
this.bidOrders = bidOrders;
}
@ManyToMany //FIXME should probably be many to many - done
public Collection<Portfolio> getPortfolios() {
return portfolios;
}
public void setPortfolios(Collection<Portfolio> portfolios) {
this.portfolios = portfolios;
}
}
答案 0 :(得分:0)
@Entity
public class Portfolio
{
@Id
@GeneratedValue
private int id;
@ManyToMany
@JoinTable( name = "PortfolioMember",
@JoinColumns : @JoinColumn( name = "Portfolio_ID", referencedColumnName="id" ),
@InverseJoinColumns : @JoinColumn( name = "Member_ID", referencedColumnName="id" )
)
private List<Member> members;
}
@Entity
public class Member
{
@Id
@GeneratedValue
private int id;
@ManyToMany( mappedBy = members )
private List<Portfolio> portfolios;
}