![](/img/trans.png)
[英]How to map a String to a java method reference for unknown return type?
[英]How to map java reference type to database column type
假设我有这样的课程
@Entity
public class Customer implements Serializable {
private int id;
private String firstName;
private String lastName;
private Referee referee;
private Collection<Address> addresses;
private Collection<Account> accounts;
.....
} //end of class Customer
@Entity
public class Referee implements Serializable {
private int id;
private String name;
private String comments;
....
} //end of class Referee
@Entity
public class Account implements Serializable {
private int id;
private double balance;
private String accountType;
...
}
@Entity
public class Address implements Serializable {
private int id;
private String addressLine;
private String country;
private String postCode;
...
}
现在,如果我在名称Customer的数据库中创建Table。 那么我将对Referee
, Collection<Address>
和Collection<Account>
类型使用哪种类型。 就像我创建列ID一样,它的类型将是INTEGER
,字符串更改为VARCHAR
但是我使用什么类型的Collection和Referee? 谢谢
Referee字段在数据库中将是整数,因为它必须存储类型为int的Referee类的id字段。
集合也将基于类的ID。 对于1到N关系,可以使用N侧的列。 因此,如果一个客户有多个地址,但是一个地址永远不属于一个客户,那么您可以将客户的ID存储在地址表中以存储关系。 如果从N到M,则需要一个额外的所谓的联接表,该表存储成对的客户ID和地址ID。 帐户内容相同。
BTW你看到他们的手册有多少JPA提供商可以生成表。
如果您是JPA的新手,那么您应该得到一本很好的书。 一些教程不会为您提供足够的深入知识,无法很好地使用它。 我个人已经阅读了Pro EJB 3:Java Persistence API。
在学习JPA时,netbeans IDE可以提供很大的帮助,并且它会为您提供许多有用的提示来注释您的实体。 例如,它将注意到您的集合需要其他注释,并且将为您提供一对多或多对多以及单向或双向的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.