2013-04-20 144 views
1

我是相當新的Hibernate和事實證明,它不是一個簡單的技術學習...在我使用休眠版本4.2.0.CR1項目。我試圖爲所有數據庫實體創建一個基類,因爲它們都應該包含一些標識符和創建日期。奇怪的是,起初,我在沒有任何基類的情況下創建了類User和UserPicture,它工作得很好,現在我添加了它,儘管它應該像以前一樣工作,但它不會o_O並繼續投擲關於我的照片列表中的一些奇怪的例外,這是以前沒有拋出...... 所以我一直在得到以下堆棧跟蹤:無法確定類型:java.util.List的

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: User, for columns: [org.hibernate.mapping.Column(profilePicture)] 
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314) 
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292) 
at org.hibernate.mapping.Property.isValid(Property.java:239) 
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:469) 
at org.hibernate.mapping.UnionSubclass.validate(UnionSubclass.java:61) 
at org.hibernate.cfg.Configuration.validate(Configuration.java:1283) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1734) 
at love.commons.database.DBManager.<init>(DBManager.java:28) 
at love.commons.database.DBManagerTest.<clinit>(DBManagerTest.java:19) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

AbstractEntity:

@Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class AbstractEntity implements Serializable{ 

private static final long serialVersionUID = 1L; 

protected Long id; 

protected Date creationDate = new Date(); 

@Id 
@Column(name="id") 
@GeneratedValue(strategy=GenerationType.TABLE) 
public Long getId() { 
    return id; 
} 

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

@Column 
@NotNull 
@Temporal(TemporalType.DATE) 
public Date getCreationDate() { 
    return creationDate; 
} 

public void setCreationDate(Date creationDate) { 
    this.creationDate = creationDate; 
} 
} 

用戶:

@Entity 
@Table(name="User") 
public class User extends AbstractEntity { 

private static final long serialVersionUID = 1L; 

@Column (unique=true, length=30) 
@NotNull 
private String login; 

@Column (length=32) 
@NotNull 
private String password; 

@NotNull 
@Email 
@Column (unique=true, length=80) 
private String email; 

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="owner") 
private List<UserPicture> profilePictures = new LinkedList<UserPicture>(); 

public String getLogin() { 
    return login; 
} 

public void setLogin(String login) { 
    this.login = login; 
} 

public String getPassword() { 
    return password; 
} 

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

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

@Transient 
public void encryptPassword() { 
    this.password = md5(password); 
} 

public List<UserPicture> getProfilePicture() { 
    return Collections.unmodifiableList(profilePictures); 
} 

public void addProfilePicture(UserPicture profilePicture) { 
    profilePicture.setOwner(this); 
    profilePictures.add(profilePicture); 
} 

@Transient 
private String md5(String input) { 

    String md5 = null; 

    if(null == input) return null; 

    try { 
     MessageDigest digest = MessageDigest.getInstance("MD5"); 
     digest.update(input.getBytes(), 0, input.length()); 
     md5 = new BigInteger(1, digest.digest()).toString(16); 

    } catch (NoSuchAlgorithmException e) { 

     e.printStackTrace(); 
    } 
    return md5; 
} 
} 

UserPicture:

@Entity 
public class UserPicture extends AbstractEntity { 

private static final long serialVersionUID = 1L; 

@Column(length=734004) 
private byte [] picture = null; 

@ManyToOne(fetch=FetchType.LAZY) 
@Column(name="owner") 
@JoinColumn(nullable=false,name="id") 
private User owner; 

public UserPicture() { 
    picture = null; 
} 

public UserPicture(InputStream stream) { 
    try { 
     this.picture = new byte[stream.available()]; 
     stream.read(picture); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public UserPicture(byte [] picture) { 
    this.picture = picture; 
} 

public byte[] getPicture() { 
    return picture; 
} 

public void setPicture(byte[] picture) { 
    this.picture = picture; 
} 

public User getOwner() { 
    return owner; 
} 

public void setOwner(User owner) { 
    this.owner = owner; 
} 
} 

那我做錯了嗎?爲什麼我要繼續獲得例外?

+0

這可能幫助:HTTP:/ /stackoverflow.com/questions/3774198/how-to-make-this-tutorial-work-could-not-determine-type-for-java-util-list-at – NINCOMPOOP 2013-04-20 07:42:53

+0

「爲什麼我不斷獲取例外呢?」導致您一次又一次地繼續運行錯誤的代碼。 – Vitaly 2013-04-20 07:54:34

回答

5

AbstractEntity不得與@Entity@Inheritance進行註釋。它必須註明@MappedSuperclass。事實上,這種繼承只是用來繼承公共屬性,這就是MappedSuperclass的用途。

你唯一的例外是在映射批註的位置,缺乏連貫性造成的。基類超類註釋了獲取者,而子類註釋了這些字段。 Hibernate使用Id註釋的位置來確定實體的訪問類型。由於@Id位於getter上,它只考慮放置在getter上的註釋,並忽略放置在字段上的註釋。把所有的註釋放在字段上(我會推薦)或者放在getter上。

此外,您的getter命名不正確。它應該是getProfilePictures()而不是getProfilePicture()

+0

現在我得到了:實體映射中的重複列:UserPicture列:id(應該用insert =「false」update =「false」映射) – 2013-04-20 07:53:08

+0

你做了什麼改變?您是否已將所有映射註釋移至字段? – 2013-04-20 07:54:39

+0

提到的所有內容+我從UserPicture.owner中刪除了@Column – 2013-04-20 07:55:59

0

Hibernate 5.2 documentation

默認情況下,@Id註釋的位置給出了默認的 訪問策略。

對於你的情況,Hibernate將使用AccessType.PROPERTY既爲UserPictureUser實體因此例外,要使用字段映射策略,你應該定義@Access戰略明確:

@Entity 
@Table(name="User") 
@Access(AccessType.FIELD) 
public class User extends AbstractEntity { 
    ... 
} 

@Entity 
@Access(AccessType.FIELD) 
public class UserPicture extends AbstractEntity { 
    .... 
} 
相關問題