简体   繁体   中英

Embeddable/ ManyToOne/ OneToMany not working

I' m quite knew to Java and currently I am working on a ChatProgramm. So I want to create a table Messages embedded with the ID (USERNUMBER) of my table Contacts using Injections. Here' s the class of my Message:

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

And here my contacts:

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

//......

So in my Message, I get two errors: @ManyToOne throws : Target entity "java.lang.String" is not an Entity @EmbeddedID throws : de.nts.data.Contact is not mapped as an embeddable

So I googled for a while.. and found something abouta orm.xml which I hadn't have. And even if I create one, @EmbeddedID throws:Embedded ID class should include method definitions for equals() and hashcode() and the orm.xml Attribute "usernumber" has invalid mapping type in this context.

Can anyone please help?

Try

@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;
  ..
}

maybe as a starting point, but as JB Nizet suggested, start with some simple JPA/Java demos to get the basics first and build up. Your example has many more errors then just what the exception was showing, none of which are solved by just throwing in an ORM.xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM