2017-09-30 71 views
1

我的項目中存在一對一外鍵關係映射問題。一對一外鍵

User.java

@Access(AccessType.FIELD) 
@Entity 
@Table(name = "users") 
public class User { 
public Role ro; 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
public int user_id; 
@Column(name = "username") 
public String username; 
@Column(name = "password") 
public String password; 


@OneToOne(cascade = CascadeType.ALL) 
@JoinColumn(name = "username") 
public Role getRo() { 
    return ro; 
} 

public void setRo(Role ro) { 
    this.ro = ro; 
} 

@Column(name = "enabled") 

public int enabled; 


public User() { 
} 

public int getUser_id() { 
    return user_id; 
} 

public User(String username, String password, int enabled) { 
    this.username = username; 
    this.password = password; 
    this.enabled = enabled; 

} 


public String getUsername() { 
    return username; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 

public String getPassword() { 
    return password; 
} 

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

public int getEnabled() { 
    return enabled; 
} 

public void setEnabled(int enabled) { 
    this.enabled = enabled; 
} 



@Override 
public String toString() { 
    return String.format(
      "student[id=%d, name='%s', age='%d']", 
      ro.getUsername(), username, ro.getUsername()); 
} 

} 

Role.java

import javax.persistence.*; 

@Access(AccessType.FIELD) 
@Entity 
@Table(name = "user_roles") 
public class Role { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
public int user_role_id; 
@Column(name = "username") 
public String username; 
@Column(name = "role") 
public String role; 

public User user; 


@OneToOne(mappedBy = "ro") 
public User getUser() { 
    return user; 
} 

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



public Role(String username, String role) { 
    this.username = username; 
    this.role = role; 
} 

public Role() { 
} 

public int getUser_role_id() { 
    return user_role_id; 
} 



public String getUsername() { 
    return username; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 

public String getRole() { 
    return role; 
} 

public void setRole(String role) { 
    this.role = role; 
} 



} 

這是錯誤:

產生的原因:org.hibernate.MappingException:無法確定類型:COM。 company.project.models.Role,在表:用戶,用於列:[org.hibernate.mapping.Column(RO)] 在org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431)〜[hibernate-核心- 5.0.12.Final.jar:5.0.12.Final] at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398)〜[hibernate-core-5.0.12.Final.jar:5.0.12。最終] 在org.hibernate.mapping.Property.isValid(Property.java:225)〜[冬眠核-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.mapping.PersistentClass。驗證(PersistentClass.java:595)〜[冬眠核-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.mapping.RootClass.validate(RootClass.java:265)〜[hibernate-核 - 5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)〜[冬眠核-5.0.12.Final.jar: 5.0.12.Final] 在org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443)〜[冬眠核-5.0.12.Final.jar:5.0.12.Final] 在有機hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)〜[冬眠-的EntityManager-5.0.12.Final.jar:5.0.12.Final] ... 22個共同幀省略

+0

嘗試在變量或用戶類的getter中註釋所有'@ Column'和'@ MappedBy'。 –

回答

2

您應該在變量聲明或getter() s上標註所有@Column/@OneToOne。這就是爲什麼冬眠在class User中找不到Role的原因。

+0

謝謝我現在有這個問題沒有爲實體指定標識符:com.company.project.models.User。這是我的代碼@Id @GeneratedValue(策略= GenerationType.AUTO) 公衆詮釋getUser_id(){ 回報USER_ID; } –

+0

你確定你不能混合字段和方法註釋嗎?我同意這樣做是不好的做法。 – Christine

+0

我相信你不能爲'@ Column'註釋,而不是完全確定映射。確實是不好的做法謝謝。 –