3

我读过@transient 和transient 关键字之间的讨论:为什么JPA 有@Transient 注释?

但是,当我使用 java 关键字而不是 @Transient 表示法使某些字段瞬态时,这些字段不会在创建表时在我的表中创建。为什么是这样?

这是我的 persistence.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="someDB" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>somewhere.classnameA</class>
        <class>somewhere.classnameB</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost:3306/project" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />

            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="both" />
        </properties>

这是一个示例实体:

import java.sql.Timestamp;

import com.google.gwt.user.client.rpc.IsSerializable;

@Entity
public class Session implements IsSerializable{

    @Id
    @Basic(optional = false)
    @Column(length = 36)
    private String sessionID;

    @Version
    @Basic(optional = false)
    transient private Timestamp lastModification;

    @Basic(optional = false)
    transient private Timestamp expireTime;

    @OneToOne(optional = false)
    private User user;

    protected Session(){

    }

    // constructor server side
    public Session(String sessionID, User user, Timestamp expireTime){
        this.sessionID = sessionID;
        this.user = user;
        this.expireTime = expireTime;
    }

    public String getSessionID() {
        return sessionID;
    }

    public void setSessionID(String sessionID) {
        this.sessionID = sessionID;
    }

    public Timestamp getLastModification() {
        return lastModification;
    }

    public void setLastModification(Timestamp lastModification) {
        this.lastModification = lastModification;
    }

    public Timestamp getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Timestamp expireTime) {
        this.expireTime = expireTime;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    @Transient
    public String toString() {
        String userID = (user != null) ? String.valueOf(user.getUserID()) : "?";
        return String.format("(%s)%s", userID, sessionID);
    }


}

注意:在上面的文件中,我删除了一些不重要的导入。在生成的表中只有两个字段,即 SESSIONID 和 USER_USERID。我还使用了持久化 api 1.0

4

3 回答 3

6

从 JPA 的角度来看,注释和修饰符是完全等价的,并且都意味着该字段不是持久的。请参阅JSR 220 规范的第 2.1.1 段,其中说:

If the entity has field-based access, the persistence provider runtime accesses
instance variables directly. All non-transient instance variables that are not
annotated with the Transient annotation are persistent.
于 2013-01-08T15:11:20.623 回答
0

瞬态字段不参与持久化,并且它们的值永远不会存储在数据库中,类似于 Java 中不参与序列化的瞬态字段

于 2014-02-24T05:52:38.290 回答
0

书中的答案 - Pro JPA 掌握 JPA

.. 我们使用了transient 修饰符而不是@Transient 注释,因此如果Employee(例如实体)从一个VM 序列化到另一个VM,那么该字段将被重新初始化以对应于新VM 的语言环境。如果在序列化过程中应保留非持久值,则应使用注释而不是修饰符。

于 2014-11-21T09:42:14.853 回答