我们可以使用 hibernate 使用 java pojo 仅映射几个表列吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21018969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we map only a few table columns with java pojo using hibernate
提问by KP_JavaDev
Using hibernate with java, do we need to map all the java pojo fields to the Database table columns? Or can we just map only a few fields with few columns?
在java中使用hibernate,我们是否需要将所有java pojo字段映射到数据库表列?或者我们可以只用几列映射几个字段?
回答by SirRichie
I assume you want to persist only a subset of a class's fields in the database.
You can use the @Transient
annotation to mark fields you do not wish to be persisted.
我假设您只想在数据库中保留类字段的子集。您可以使用@Transient
注释来标记您不希望保留的字段。
Warning: make sure to respect that those fields might not be initialized (since there is no value for them in the DB when they are loaded)
警告:确保尊重这些字段可能不会被初始化(因为它们在加载时在数据库中没有值)
回答by MateuszPrzybyla
I didn't check it but I don't see why couldn't you leave some fields not mapped, especially if they do not exist as a column in the table. Of course in some cases you need to map a column to the field, e.g. when column cannot be null, during saving you will get an exception.
我没有检查它,但我不明白为什么你不能留下一些未映射的字段,特别是如果它们不作为表中的列存在。当然,在某些情况下,您需要将列映射到字段,例如,当列不能为空时,在保存过程中您会遇到异常。
回答by Olimpiu POP
In hibernate all the fields will be mapped if not described in any other way. So one can point to the ORM mapper not to map given fields to the DB by using the
在休眠中,如果没有以任何其他方式描述,所有字段都将被映射。因此可以指向 ORM 映射器,而不是通过使用
[@Transient][1] annotation in case JPA is used
or even the **transient** keyword from java - careful when using this one, it will prevent the given field to be serialized
回答by Prabha
Yes you can map few fields of your Pojo class to your table columns. Not a Problem. It will successfully store the data in the DB.
是的,您可以将 Pojo 类的几个字段映射到表列。不是问题。它将成功地将数据存储在数据库中。
Example:
例子:
Below is StudentData Pojo
下面是 StudentData Pojo
public class StudentData1 {
private String name;
private int id;
private String name1;
//setters & getters
}
And HBM file:
和 HBM 文件:
<class name="example.StudentData" table="StudentData">
<id name="id" column="pid" >
<generator class="assigned" />
</id>
<property name="name" column="pname" />
</class>
And the CFG file is
CFG文件是
<mapping resource="StudentData.hbm.xml"/>
And the Main Class is
主类是
public static void main(String[] args) {
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session = factory.openSession();
StudentData1 s = new StudentData1();
s.setId(1);
s.setName("iPhone");
Transaction tx = session.beginTransaction();
session.save(s);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
You can run this code it, will execute and store only two fields.
你可以运行这段代码吧,只会执行和存储两个字段。