我是 JPA 的新手。
我需要将搜索对象映射到表。 搜索对象只有id、name。大对象有更多的字段 id、名称、地址等。
我用它作为大对象 查看纯文本到剪贴板打印?
我用它作为大对象
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String adress;
private String keywords;
}
//this is my search object
@XXX
public class UserSearch {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
}
我需要使用哪些注释将搜索对象映射到表用户?
我使用的是spring+struts2+hibernate+JPA。
感谢帮助! 谢谢!
最佳答案
What annotations I need to use to map the search object to the table users?
如果要将其映射到不默认为实体名称的表,则需要使用@Table
注释:
@Entity
@Table(name = "Users")
public class UserSearch {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
}
但是,如果此类用于搜索结果并且您不打算保留 UserSearch
实例,则实际上不需要将其设为实体。假设您提供适当的构造函数:
public class UserSearch {
private long id;
private String name;
public UserSearch(long id, String name) {
this.id = id;
this.name = name;
}
}
您可以将此类与 SELECT NEW 结合使用,如下所示:
SELECT NEW com.acme.UserSearch(u.id, u.name) FROM User u
关于java - 如何使用 JPA 注释将搜索对象映射到具有更多字段的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2930434/