2015-11-01 279 views
1

我遇到了映射到同一個實體的問題。實體代表樹節點,它假定父母和孩子由同一實體定義:JPA實體映射 - 鏈接相同的實體

編號級

@MappedSuperclass 
public class ParentId { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "ID") 
    private long id; 

    public void setId(long id) { 
     this.id = id; 
    } 
    public long getId() { 
     return id; 
    } 
} 

實體類

@Entity 
@Table(name = "NAVIGATION_TREE_NODE") 
public class NavigationTreeNode extends ParentId { 

    @Column(name = "NODE_NAME") 
    private String nodeName; 

    @Column(name = "NODE_TYPE") 
    @Enumerated(EnumType.ORDINAL) 
    private NodeType nodeType; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "ENTITY_ID") 
    private NavigationTreeNode parent; 

    @OneToMany(mappedBy = "parent") 
    private List<NavigationTreeNode> children; 

    public String getNodeName() { 
     return nodeName; 
    } 

    public void setNodeName(String nodeName) { 
     this.nodeName = nodeName; 
    } 

    public NodeType getNodeType() { 
     return nodeType; 
    } 

    public void setNodeType(NodeType nodeType) { 
     this.nodeType = nodeType; 
    } 

    public void setParent(NavigationTreeNode parent) { 
     this.parent = parent; 
    } 

    public NavigationTreeNode getParent() { 
     return parent; 
    } 

    public void setChildren(List<NavigationTreeNode> children) { 
     this.children = children; 
    } 

    public List<NavigationTreeNode> getChildren() { 
     return children; 
    } 

    public enum NodeType { 
     ROOT, 
     NODE, 
     LEAF; 

     public String getName() { 
      return this.name(); 
     } 
    } 
} 

SQL

CREATE TABLE "DEV1"."NAVIGATION_TREE_NODE" 
    ( "ID" NUMBER NOT NULL ENABLE, 
    "NODE_NAME" VARCHAR2(30 BYTE), 
    "NODE_TYPE" VARCHAR2(20 BYTE), 
    "PARENT_ID" NUMBER, 
    "ENTITY_ID" NUMBER, 
    PRIMARY KEY ("ENTITY_ID") 
    USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
    STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 
    PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) 
    TABLESPACE "SYSTEM" ENABLE, 
    FOREIGN KEY ("PARENT_ID") 
     REFERENCES "DEV1"."NAVIGATION_TREE_NODE" ("ENTITY_ID") ENABLE 
    ) SEGMENT CREATION IMMEDIATE 
    PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING 
    STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 
    PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) 
    TABLESPACE "SYSTEM" ; 

    CREATE OR REPLACE TRIGGER "DEV1"."NAVIGATION_TREE_NODE_TRIGGER" 
    before insert on NAVIGATION_TREE_NODE 
    for each row 
begin 
    select dev1_seq.nextval 
    into :new.id 
    from dual; 
end; 

當我嘗試編譯它時拋出以下異常:

Caused by: java.sql.SQLException: ORA-17059 Fail to convert to internal representation 
    at oracle.jdbc.driver.CharCommonAccessor.getInt(CharCommonAccessor.java:147) 
    at oracle.jdbc.driver.T4CVarcharAccessor.getInt(T4CVarcharAccessor.java:830) 
    at oracle.jdbc.driver.OracleResultSetImpl.getInt(OracleResultSetImpl.java:942) 
    at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:438) 
    at org.hibernate.type.EnumType$OrdinalEnumValueMapper.getValue(EnumType.java:372) 
    at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:107) 
    at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:127) 
    at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106) 
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969) 
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1696) 
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1628) 
    at org.hibernate.loader.Loader.getRow(Loader.java:1515) 
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:726) 
    at org.hibernate.loader.Loader.processResultSet(Loader.java:953) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:921) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355) 
    at org.hibernate.loader.Loader.doList(Loader.java:2554) 
    ... 35 more 

請問如何解決這個問題?謝謝。

+0

「Сбойпреобразованиявовнутреннеепредставление」 不是很理解的非俄羅斯用戶。請爲此提供英文翻譯,否則這裏的大多數程序員將無法提供幫助。最佳:編輯原始帖子並將其替換。 – MWiesner

+1

ORA-17059無法轉換爲內部表示 –

回答

3

你有

@Column(name = "NODE_TYPE") 
@Enumerated(EnumType.ORDINAL) // <-- as a number 
private NodeType nodeType; 

但在你的SQL是

"NODE_TYPE" VARCHAR2(20 BYTE). 
+0

您是對的。它解決了! – aime