是否可以将对象映射到现有表的一部分?
是否可以将对象仅映射到数据库中现有表的一部分?例如:
public class Account {
private Integer id;
private Integer accountNumber;
@Id
public Integer getId() {
return this.id;
}
@Column(nullable=false)
public Integer getAccountNumber() {
return this.accountNumber;
}
}
在数据库中(只是为了问题):
Account
- id
- accountnumber
- lastmodified
- localbranchid
Is it possible to map an object to only part of an existing table in a database? For example:
public class Account {
private Integer id;
private Integer accountNumber;
@Id
public Integer getId() {
return this.id;
}
@Column(nullable=false)
public Integer getAccountNumber() {
return this.accountNumber;
}
}
In the database (just for the sake of the question):
Account
- id
- accountnumber
- lastmodified
- localbranchid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您只能将数据库表中的一部分列映射到
Account
类(您只需映射您感兴趣的列),但是如果您需要插入新的 < code>Accounts 进入数据库并且未映射的列具有非空约束并且没有数据库默认值,您将遇到问题。Yes, you can only map a portion of the columns in the database table to the
Account
class (you simply just map the columns you are interested in), but if you ever have a need to insert newAccount
s into the database and the unmapped columns have not-null constraints and no database default values, you will run into problems.在关系映射中遗漏字段应该不会有任何问题。实际上只有一条规则。如果该类是可更新的,那么您需要包含构成主键的所有字段,以便更新可以传播到数据库。
There shouldn't be any problems leaving out fields in relational mapping. There is really only one rule. If the class is to be updateable then you need to include all of the fields that make up the primary key so the updates can be propagated to the database.