@Entity
@Table(name = "your_entity")
public class YourEntityClass implements IEntity<Long> {
@Id
@SequenceGenerator(name = "gen_test_entity2_id", sequenceName = "test_entity2_id_seq", allocationSize = 10)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "gen_test_entity2_id")
private Long id;
@Column(name = "name", nullable = false, length = 64)
private String name;
/*
* Create constructors, getters, setters, isEquals, hashcode, etc.
*/
}
public interface IEntity<I extends Serializable> extends Serializable {
/**
* Property which represents id.
*/
String P_ID = "id";
/**
* Get primary key.
*
* @return primary key
*/
I getId();
/**
* Set primary key.
*
* @param id primary key
*/
void setId(I id);
}
上面的代碼,我的問題是,爲什麼YourEntityClass
需要通過Long
在IEntity<Long>
?爲什麼不能像IEntity<String>
?是因爲id
的類型是Long
,並且id
的獲取者必須返回我們提供給IEntity
的同一類型?瞭解泛型
+1給出第二個例子。 – 2009-12-30 03:12:55
非常清楚地理解=) – cometta 2009-12-30 03:25:04