Embeddable / ManyToOne / OneToMany无法正常工作

时间:2016-06-16 14:08:04

标签: java jpa persistence

我对Java很了解,目前我正在开发一个ChatProgramm。 所以我想使用Injections创建一个表格,其中嵌入了我的表格的ID(USERNUMBER)。 这是我的消息类:

@Embeddable
@Entity(name = "MESSAGE")
public class Message implements Serializable {


@ManyToOne
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "INCOME_MESSANGE", nullable = false)
private String incomingMessage;

@EmbeddedId
@JoinColumn(name = "USERNUMBER", nullable = false)
private Contact contact;
ChatApplicationRemote chatApplicationRemote;

public Message(String ip, String msg) throws IOException {
    incomingMessage = msg;
    contact = chatApplicationRemote.getcontactByIP(ip.toString());
}

public Message(){

}

public String getIncomingMessage() {
    return incomingMessage;
}

public Contact getContact() {
    return contact;
}

在这里我的联系人:

@Entity(name = "CONTACTS")
@Embeddable
public class Contact implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -6855140755056337926L;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "PRENAME", nullable = false)
private String vorname;
@Column(name = "IP", nullable = false)
private String ip;
@Column(name = "PORT", nullable = false)
private Integer port;
@Id
@OneToMany(mappedBy = "Message.incomingMessage")
@Column(name = "USERNUMBER", nullable = false)
private String usernumber;

public Contact(String usernumber, String name, String vorname, String ip, String port) {
    super();
    this.usernumber = usernumber;
    this.name = name;
    this.vorname = vorname;
    this.ip = ip;
    this.port = Integer.parseInt(port);
}

public Contact(){

}

public String getUsernumber() {
    return usernumber;
}

//......

所以在我的消息中,我收到两个错误: @ManyToOne抛出:目标实体“java.lang.String”不是实体 @EmbeddedID抛出:de.nts.data.Contact未映射为可嵌入的

所以我用谷歌搜索了一段时间......发现了一些我没有的orm.xml。即使我创建了一个,@ EmbridID也会抛出:嵌入式ID类应包含equals()和hashcode()的方法定义,而orm.xml属性“usernumber”在此上下文中具有无效的映射类型。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

尝试

@Entity
public class Message implements Serializable {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;

  @Column(name = "INCOME_MESSANGE", nullable = false)
  private String incomingMessage;

  @ManyToOne
  @JoinColumn(name = "USERNUMBER", nullable = false)
  private Contact contact;

  @Transient
  ChatApplicationRemote chatApplicationRemote;
  ..
}


@Entity
public class Contact implements Serializable {

  private static final long serialVersionUID = -6855140755056337926L;

  @Column(name = "NAME", nullable = false)
  private String name;
  @Column(name = "PRENAME", nullable = false)
  private String vorname;
  @Column(name = "IP", nullable = false)
  private String ip;
  @Column(name = "PORT", nullable = false)
  private Integer port;
  @Id
  @Column(name = "USERNUMBER", nullable = false)
  private String usernumber;

  @OneToMany(mappedBy = "incomingMessage")
  private LIst<Message> messages;
  ..
}

可能作为一个起点,但正如JB Nizet建议的那样,从一些简单的JPA / Java演示开始,首先获得基础知识并进行构建。您的示例有更多错误,然后只是异常显示的内容,只有抛出一个ORM.xml才能解决这些错误。