我正在閱讀關於Hibernate的一些面試問題,並且遇到了Hibernate派生的屬性。我正在嘗試使用@Formula註釋的簡單示例,但它不起作用。任何人都可以告訴我我錯過了什麼。代碼片段低於@Formula模式在休眠
輸出和SQL查詢顯示在最後。
實體(Employee.java)
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Formula;
@Entity
@Table(name="EMPLOYEE")
public class Employee implements java.io.Serializable {
private static final long serialVersionUID = -7311873726885796936L;
@Id
@Column(name="ID")
private Integer id;
@Column(name="FIRST_NAME", length=31)
private String firstName;
@Column(name="LAST_NAME", length=31)
private String lastName;
@Column(name="MONTHLY_SALARY")
private float monthlySalary;
@Formula("MONTHLY_SALARY*12")
private float yearlySalary;
public Employee() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public float getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(float monthlySalary) {
this.monthlySalary = monthlySalary;
}
public float getYearlySalary() {
return yearlySalary;
}
}
的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.jdbc.use_streams_for_binary">true</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.max_fetch_depth">3</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name ="show_sql">true</property>
<mapping class="Employee"/>
<!-- <mapping class="dto.UserDetailsEmbeddedId"/>-->
</session-factory>
</hibernate-configuration>
Tester類 進口的org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; 公共類HibernateTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory factory = (new Configuration()).configure().buildSessionFactory();
Session session = factory.openSession();
Employee employee = new Employee();
employee.setFirstName("Tarun");
employee.setLastName("bhatt");
employee.setMonthlySalary(34000);
employee.setId(12);
session.beginTransaction();
session.save(employee);
session.getTransaction().commit();
System.out.println("salary1 = "+employee.getYearlySalary());
session.close();
System.out.println("salary = "+employee.getYearlySalary());
}
}
輸出 salary1 = 0.0 薪水= 0.0
查詢 休眠:創建表EMPLOYEE(ID號碼(10,0)不null,FIRST_NAME varchar2(31 char),LAST_NAME varchar2(31 char),MONTHLY_SALARY float,主鍵(ID)) Hibernate:insert into EMPLOYEE(名字,姓氏,MONTHLY_SALARY,ID)的值(?,?,?,?)