hibernate注释创建关系

时间:2014-04-16 21:43:30

标签: hibernate jpa annotations relationship

我在User和Task之间有一个关系,用户可以分配给一个或多个任务,但不能分配到相同的持续时间,任务可以由单个或多个用户执行,这就是我添加关联表MembreTache和I的原因不知道它是否正确,这里的类membertache tache和user: 我有这个错误:

ERROR: org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000388: Unsuccessful: create table User (idUser  bigserial not null, dateDerniereConnexion timestamp, email varchar(255), grade varchar(255), login varchar(255), mdp varchar(255), nomUser varchar(255), tel varchar(255), idEquipe int8, idRole int8, primary key (idUser))
ERROR: org.hibernate.tool.hbm2ddl.SchemaUpdate - ERREUR: erreur de syntaxe sur ou près de « User »
  Position : 14
ERROR: org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000388: Unsuccessful: alter table MembreTache add constraint FK4EC6FEB35B0BF9EE foreign key (idUser) references User
ERROR: org.hibernate.tool.hbm2ddl.SchemaUpdate - ERREUR: erreur de syntaxe sur ou près de « User »
  Position : 91

这是我的类us​​er.java:

   public class User implements Serializable {

        private static final long serialVersionUID = 3954206400422372693L;
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long idUser;
        @ManyToOne
        @JoinColumn(name="idRole")
        private Role role;
        private String nomUser;
        private String grade;
        private String tel;
        private String email;
        private Date dateDerniereConnexion;
        private String login;
        private String mdp;
        @ManyToOne
        @JoinColumn(name="idEquipe")
        private EquipeProjet equipe;
        @OneToMany(mappedBy="user")
        private Collection<MembreTache> membreTache;
                        /*----------*/
        public User(Role role, String nomUser, String grade, String tel, String email,
                Date dateDerniereConnexion, String login, String mdp) {
            super();
            this.role = role;
            this.nomUser = nomUser;
            this.grade = grade;
            this.tel = tel;
            this.email = email;
            this.dateDerniereConnexion = dateDerniereConnexion;
            this.login = login;
            this.mdp = mdp;
        }


    }

and this is the class EquipeProjet:

    package com.gestion.projet.entities;

    @Entity
    @Table(name="EquipeProjet")
    public class EquipeProjet implements Serializable{
        /**
         * 
         */
        private static final long serialVersionUID = 2L;
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long idEquipe;
        private int nbrMmbre;
        @OneToMany(mappedBy="equipe")
        private Collection<User> user;


        public Long getIdEquipe() {
            return idEquipe;
        }

        public EquipeProjet() {
            super();

        }


    }

classe memebretache.java:

@Entity
@Table(name="MembreTache")
public class MembreTache implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 995686353843852683L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long idMembreTache;
    @ManyToOne
    @JoinColumn(name="idTache")
    private Tache tache;
    @ManyToOne
    @JoinColumn(name="idUser")
    private User user;
    private int travailEffecuter;
    private int travailRestant;

    /*-------le constructeur,les getter et setter------*/

    public Long getIdMembreTache() {
        return idMembreTache;
    }

    public int getTravailEffecuter() {
        return travailEffecuter;
    }
    public void setTravailEffecuter(int travailEffecuter) {
        this.travailEffecuter = travailEffecuter;
    }
    public int getTravailRestant() {
        return travailRestant;
    }
    public void setTravailRestant(int travailRestant) {
        this.travailRestant = travailRestant;
    }
    public MembreTache() {
        super();

    }

    public MembreTache( Tache tache, User user,
            int travailEffecuter, int travailRestant) {
        super();

        this.tache = tache;
        this.user = user;
        this.travailEffecuter = travailEffecuter;
        this.travailRestant = travailRestant;
    }


}

这是类tache.java:

@Entity
@Table(name="Tache")
public class Tache implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 8616227436464089403L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long idTache;
    private String nomTache;
    private String statusTache;
    private Date dateDebut;
    private Date dateFin;
    @ManyToOne
    @JoinColumn(name="parent_tache_id")
    private Tache tacheParente;
    private Long predecesseur;
    private Long durre;
    private String commentaire;
    private String type ;
    private boolean confidentialite;
    @ManyToOne
    @JoinColumn(name="idPhase")
    private Phase phase;
    @OneToMany(mappedBy="tache")
    private Collection<MembreTache> membreTache;

    public Tache(String nomTache, String statusTache, Date dateDebut,
            Date dateFin, Tache tacheParente, Long predecesseur, Long durre,
            String commentaire, String type, boolean confidentialite) {
        super();
        this.nomTache = nomTache;
        this.statusTache = statusTache;
        this.dateDebut = dateDebut;
        this.dateFin = dateFin;
        this.tacheParente = tacheParente;
        this.predecesseur = predecesseur;
        this.durre = durre;
        this.commentaire = commentaire;
        this.type = type;
        this.confidentialite = confidentialite;
    }

    public String getNomTache() {
        return nomTache;
    }






}

1 个答案:

答案 0 :(得分:0)

我不确定'user'是不是数据库中的保留字。这是我发现使用这样一个单词的一个例子,但我不确定这是否有效。在我看来,最好避免分配这样的名字。

@Column(name = "`from`")
private String from;

SQL Key Words