2011-06-04 95 views
1

我試圖設置與JPA註釋的外鍵關係,並使用Hibernate生成表格,但出現錯誤,無法看到原因。我在User中執行主要方法來生成表格。爲什麼不能在JPA和Hibernate中使用外鍵關係?

這裏的錯誤 -

INFO: Hibernate Validator not found: ignoring 
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: com.site.model.Name, at table: USER, for columns: [org.hibernate.mapping.Column(name)] 
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306) 
at org.hibernate.mapping.Column.getSqlTypeCode(Column.java:164) 
at org.hibernate.mapping.Column.getSqlType(Column.java:208) 
at org.hibernate.mapping.Table.sqlCreateString(Table.java:418) 
at org.hibernate.cfg.Configuration.generateSchemaCreationScript(Configuration.java:1099) 
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:131) 
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:92) 
at com.thepoliticalbottomline.model.User.main(User.java:55) 

和源代碼,

package com.sitename.model; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.OneToOne; 
import javax.persistence.PrimaryKeyJoinColumn; 
import javax.persistence.Table; 

import org.hibernate.cfg.Configuration; 
import org.hibernate.tool.hbm2ddl.SchemaExport; 

@Entity 
public class User { 
    private Long id; 
    private String password; 
    private Name name; 

    @Id 
    @GeneratedValue 
    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public void setName(Name name) { 
     this.name = name; 
    } 
    @OneToOne(cascade = CascadeType.ALL) 
    @PrimaryKeyJoinColumn 
    public Name getName() { 
     return name; 
    } 

    public static void main(String args[]) { 
     Configuration config = new Configuration(); 
     config.addAnnotatedClass(User.class); 
     config.addAnnotatedClass(Name.class); 
     config.configure(); 
     new SchemaExport(config).create(true, true); 
    } 
} 

類的名稱,

package com.sitename.model; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 

@Entity 
public class Name { 
    private Long id; 
    private String first; 
    private String middle; 
    private String last; 

    /** 
    * @return the id 
    */ 
    @Id 
    @GeneratedValue 
    public Long getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(Long id) { 
     this.id = id; 
    } 

    /** 
    * @return the first 
    */ 
    public String getFirst() { 
     return first; 
    } 

    /** 
    * @param first the first to set 
    */ 
    public void setFirst(String first) { 
     this.first = first; 
    } 

    /** 
    * @return the middle 
    */ 
    public String getMiddle() { 
     return middle; 
    } 

    /** 
    * @param middle the middle to set 
    */ 
    public void setMiddle(String middle) { 
     this.middle = middle; 
    } 

    /** 
    * @return the last 
    */ 
    public String getLast() { 
     return last; 
    } 

    /** 
    * @param last the last to set 
    */ 
    public void setLast(String last) { 
     this.last = last; 
    } 
} 

回答

2

不能在屬性和方法混合JPA註解。您只能在給定的類中註釋屬性方法。

這適用於Hibernate。我不確定其他JPA實現。

相關問題