JPA从没有做任何事情的实体生成表

时间:2012-11-04 05:01:37

标签: mysql database eclipse orm jpa-2.0

我正在使用Eclipse Indigo和Hibernate的JPA-2实现。问题是,当我使用该工具“从实体生成表”时,根本没有任何影响。我得到45个错误,说我的表不存在。 任何人都可以帮我解决这个问题,谷歌搜索没有帮助。我现在已经坚持了很长时间。

这是我的Hibernate配置

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
        <class>com.myclassregister.model.helper.enums.UserTypeEnum</class>
        <class>com.myclassregister.model.helper.enums.SystemRoleEnum</class>
        <class>com.myclassregister.model.helper.enums.SchoolRoleEnum</class>
        <class>com.myclassregister.model.helper.enums.PreferenceGroupEnum</class>
        <class>com.myclassregister.model.helper.enums.PreferenceEnum</class>
        <class>com.myclassregister.model.helper.enums.PostTypeEnum</class>
        <class>com.myclassregister.model.helper.enums.NoteTypeEnum</class>
        <class>com.myclassregister.model.helper.enums.NotePermissionEnum</class>
        <class>com.myclassregister.model.helper.enums.IssueTypeEnum</class>
        <class>com.myclassregister.model.helper.enums.IssueCategoryEnum</class>
        <class>com.myclassregister.model.helper.enums.GenderEnum</class>
        <class>com.myclassregister.model.helper.enums.CaseStatusEnum</class>
        <class>com.myclassregister.model.helper.embeddables.Recipient</class>
        <class>com.myclassregister.model.helper.embeddables.Name</class>
        <class>com.myclassregister.model.helper.embeddables.Mobile</class>
        <class>com.myclassregister.model.helper.embeddables.Landline</class>
        <class>com.myclassregister.model.helper.embeddables.BlobField</class>
        <class>com.myclassregister.model.helper.embeddables.AuditTrail</class>
        <class>com.myclassregister.model.helper.embeddables.Address</class>
        <class>com.myclassregister.model.base.users.UserCredential</class>
        <class>com.myclassregister.model.base.users.User</class>
        <class>com.myclassregister.model.base.users.Teacher</class>
        <class>com.myclassregister.model.base.users.SystemUser</class>
        <class>com.myclassregister.model.base.users.SystemAdmin</class>
        <class>com.myclassregister.model.base.users.Student</class>
        <class>com.myclassregister.model.base.users.SchoolStaff</class>
        <class>com.myclassregister.model.base.users.Principal</class>
        <class>com.myclassregister.model.base.users.Parent</class>
        <class>com.myclassregister.model.base.users.OfficeAdmin</class>
        <class>com.myclassregister.model.base.subject.Syllabus</class>
        <class>com.myclassregister.model.base.subject.Subject</class>
        <class>com.myclassregister.model.base.subject.Schedule</class>
        <class>com.myclassregister.model.base.subject.Homework</class>
        <class>com.myclassregister.model.base.subject.Grade</class>
        <class>com.myclassregister.model.base.subject.ExamSeries</class>
        <class>com.myclassregister.model.base.subject.Exam</class>
        <class>com.myclassregister.model.base.subject.Assignment</class>
        <class>com.myclassregister.model.base.preferences.UserPreference</class>
        <class>com.myclassregister.model.base.preferences.SchoolPreference</class>
        <class>com.myclassregister.model.base.preferences.ParentSMSFilter</class>
        <class>com.myclassregister.model.base.posts.PostRecipient</class>
        <class>com.myclassregister.model.base.posts.Post</class>
        <class>com.myclassregister.model.base.posts.NoteSharing</class>
        <class>com.myclassregister.model.base.posts.Note</class>
        <class>com.myclassregister.model.base.posts.Message</class>
        <class>com.myclassregister.model.base.posts.LeaveRequest</class>
        <class>com.myclassregister.model.base.misc.SchoolClass</class>
        <class>com.myclassregister.model.base.misc.School</class>
        <class>com.myclassregister.model.base.events.EventRSVP</class>
        <class>com.myclassregister.model.base.events.EventInvitee</class>
        <class>com.myclassregister.model.base.events.Event</class>
        <class>com.myclassregister.model.base.casemgmt.Contact</class>
        <class>com.myclassregister.model.base.casemgmt.Case</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/dummyissoyummy"/>
            <property name="hibernate.connection.password" value="master"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dummyissoyummy"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="master"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

实体:

import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Cascade;

import com.myclassregister.model.base.casemgmt.Contact;
import com.myclassregister.model.base.users.UserCredential;
import com.myclassregister.model.helper.enums.CaseStatusEnum;
import com.myclassregister.model.helper.enums.IssueTypeEnum;

@Entity
public class Case implements java.io.Serializable {

    private static final long serialVersionUID = 1226071621948838470L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @Column(nullable=false,unique=true)
    private Long caseID;    

    @Column(nullable=true)
    @ManyToOne(optional=true)
    @JoinColumn(name="caseOwnerID", referencedColumnName="userID", nullable=true)
    private UserCredential caseOwner;

    @Column(nullable=true)
    @ManyToOne(optional=true)
    @JoinColumn(name="caseRequestorID", referencedColumnName="userID", nullable=true)
    private UserCredential caseRequestor;

    @Column(nullable=false)
    @ManyToOne(optional=true)
    @JoinColumn(name="caseStatus", referencedColumnName="caseStatus", nullable=true)
    private CaseStatusEnum caseStatus;

    @Column(nullable=false)
    @ManyToOne(optional=true)
    @JoinColumn(name="issueType", referencedColumnName="issueType", nullable=true)
    private IssueTypeEnum issueType;

    @Column(nullable=false)
    private Integer severity=5;

    @OneToMany
    @Basic(optional=true,fetch=FetchType.LAZY)
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @JoinTable(name="caseContact", 
               joinColumns= {@JoinColumn(name="caseID",referencedColumnName="caseID")}, 
               inverseJoinColumns= {@JoinColumn(name="contactID", referencedColumnName = "contactID")}
    )
    private Set<Contact> caseContacts;

    public Case() {
    }

    public Case(Long caseID, UserCredential caseOwner,
            UserCredential caseRequestor, CaseStatusEnum caseStatus,
            IssueTypeEnum issueType, Integer severity, Set<Contact> caseContacts) {
        this.caseID = caseID;
        this.caseOwner = caseOwner;
        this.caseRequestor = caseRequestor;
        this.caseStatus = caseStatus;
        this.issueType = issueType;
        this.severity = severity;
        this.caseContacts = caseContacts;
    }

    public Long getCaseID() {
        return caseID;
    }

    public void setCaseID(Long caseID) {
        this.caseID = caseID;
    }

    public UserCredential getCaseOwner() {
        return caseOwner;
    }

    public void setCaseOwner(UserCredential caseOwner) {
        this.caseOwner = caseOwner;
    }

    public UserCredential getCaseRequestor() {
        return caseRequestor;
    }

    public void setCaseRequestor(UserCredential caseRequestor) {
        this.caseRequestor = caseRequestor;
    }

    public CaseStatusEnum getCaseStatus() {
        return caseStatus;
    }

    public void setCaseStatus(CaseStatusEnum caseStatus) {
        this.caseStatus = caseStatus;
    }

    public IssueTypeEnum getIssueType() {
        return issueType;
    }

    public void setIssueType(IssueTypeEnum issueType) {
        this.issueType = issueType;
    }

    public Integer getSeverity() {
        return severity;
    }

    public void setSeverity(Integer severity) {
        this.severity = severity;
    }

    public Set<Contact> getCaseContacts() {
        return caseContacts;
    }

    public void setCaseContacts(Set<Contact> caseContacts) {
        this.caseContacts = caseContacts;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((caseContacts == null) ? 0 : caseContacts.hashCode());
        result = prime * result + ((caseID == null) ? 0 : caseID.hashCode());
        result = prime * result
                + ((caseOwner == null) ? 0 : caseOwner.hashCode());
        result = prime * result
                + ((caseRequestor == null) ? 0 : caseRequestor.hashCode());
        result = prime * result
                + ((caseStatus == null) ? 0 : caseStatus.hashCode());
        result = prime * result
                + ((issueType == null) ? 0 : issueType.hashCode());
        result = prime * result
                + ((severity == null) ? 0 : severity.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Case other = (Case) obj;
        if (caseContacts == null) {
            if (other.caseContacts != null)
                return false;
        } else if (!caseContacts.equals(other.caseContacts))
            return false;
        if (caseID == null) {
            if (other.caseID != null)
                return false;
        } else if (!caseID.equals(other.caseID))
            return false;
        if (caseOwner == null) {
            if (other.caseOwner != null)
                return false;
        } else if (!caseOwner.equals(other.caseOwner))
            return false;
        if (caseRequestor == null) {
            if (other.caseRequestor != null)
                return false;
        } else if (!caseRequestor.equals(other.caseRequestor))
            return false;
        if (caseStatus == null) {
            if (other.caseStatus != null)
                return false;
        } else if (!caseStatus.equals(other.caseStatus))
            return false;
        if (issueType == null) {
            if (other.issueType != null)
                return false;
        } else if (!issueType.equals(other.issueType))
            return false;
        if (severity == null) {
            if (other.severity != null)
                return false;
        } else if (!severity.equals(other.severity))
            return false;
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

我是stackoverflow的新手,所以如果这是不正确的,请道歉。
你有没有考虑过改变

<property name="hibernate.hbm2ddl.auto" value="update"/> 

进入

<property name="hibernate.hbm2ddl.auto" value="create"/>

看看这是否第一次设置它?我总是使用create然后将其注释掉,否则它每次都会重新创建它,但似乎对我有效。