2017-04-05 113 views
2

我得到錯誤: 調用init方法失敗;嵌套異常是org.hibernate.MappingException:實體映射中的重複列:com.messagemanager.domain.User column:id(應該映射爲insert =「false」update =「false」)我收到錯誤:調用init方法失敗;嵌套異常是org.hibernate.MappingException:實體映射中的重複列

有我的實體:

@MappedSuperclass 
public abstract class BaseEntity{ 

    @Id 
    @Column(nullable = false, unique = true, name = "id", insertable = false, updatable = false) 
    @GeneratedValue(strategy= GenerationType.AUTO) 
    private Long id; 

    public Long getId() { 
     return id; 
    } 

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


@Entity 
@Table(name = "User") 
public class User extends BaseEntity implements Serializable{ 
    @Column(name = UserFieldsKey.ID, length = 50, nullable = false) 
    private String name; 

    @Column(updatable = false, insertable = false) 
    @Transient 
    private List<Message> listMessage = new ArrayList<>(); 

    public static class UserFieldsKey{ 
     public static final String NAME = "name"; 
    } 

    public User() { 
    } 

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "user") 
    public List<Message> getListMessage() { 
     return listMessage; 
    } 

    public void setListMessage(List<Message> listMessage) { 
     this.listMessage = listMessage; 
    } 


    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 


@Entity 
@Table(name = "Message") 
public class Message extends BaseEntity implements Serializable{ 
    @Column(name = MessageFieldsKey.MESSAGE, length = 500, nullable = false) 
    private String message; 

    @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER) 
    @JoinColumn(name="user") 
    private User user; 

    @Column(name = "id_user") 
    private Long id_user; 

    public Long getId_user() { 
     return id_user; 
    } 

    public void setId_user(Long id_user) { 
     this.id_user = id_user; 
    } 

    public static class MessageFieldsKey{ 
     public static final String MESSAGE = "mes"; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public User getUser() { 
     return user; 
    } 

    public Message() { 
    } 

    public Message(String message, User user) { 
     this.message = message; 
     this.user = user; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

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

任何想法?

P.S.非常感謝!

+0

UserFieldsKey.ID的值是什麼? –

+0

@DmitriyS是我嘗試回答的問題? – Eugene

+0

已被封閉。謝謝大家! –

回答

0

看起來那是因爲你有這樣的:

@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER) 
@JoinColumn(name="user") 
private User user; 

將映射到User.id;並在Message你已經有一個id因爲你正在延伸BaseEntity

0

問題解決了)我誤解了表之間的關係。

相關問題