2014-05-05 186 views
0

我需要一些幫助來處理奇怪的錯誤:「Fabricationfolder未映射」。Hibernate 4中的多個項目和實體:實體未映射

這是上下文: 我有一個項目「戰爭」,其中包括一個庫「jar」。他們兩個在兩個分離的模式MariaDB上都有實體。 我無法更改任何內容,因爲它被其他項目使用。 我使用相同的休眠版本:4.1.7和彈簧4.

由於某些原因,我需要將我的戰爭的一些實體鏈接到jar實體。爲了給jar的實體添加屬性,我決定將jar的一些實體擴展到我的戰爭中。 這是一個好方法嗎?

當我啓動Web應用程序時,Fabricationfolder上的第一個請求失敗,並顯示此錯誤。

這裏頃配置:

架構: CREATE DATABASE IF NOT EXISTS SAMPLEDB處理latin1 COLLATE latin1_swedish_ci 的字符集;

USE sampledb; 

CREATE TABLE IF NOT EXISTS fabricationfolder (
    id_folder INT(11) NOT NULL, 
    id_product_type INT(11) NOT NULL, 
    folder_number VARCHAR(15) NOT NULL, 
    customer_code VARCHAR(15) NOT NULL, 
    customer_name VARCHAR(32) NOT NULL, 
    brand VARCHAR(32) NOT NULL, 
    creation_date DATETIME NOT NULL, 
    bat VARCHAR(1) NOT NULL, 
    grouped VARCHAR(1) NOT NULL, 
    fabrication_limit_date DATETIME NOT NULL, 
    classification VARCHAR(15) DEFAULT NULL, 
    observation VARCHAR(250) DEFAULT NULL, 
    status VARCHAR(30) DEFAULT NULL, 
    height DECIMAL(7, 2) DEFAULT NULL, 
    flag_height INT(1) NOT NULL, 
    width DECIMAL(7, 2) DEFAULT NULL, 
    flag_width INT(1) NOT NULL, 
    intermediate_code VARCHAR(15) DEFAULT NULL, 
    date_update DATE DEFAULT NULL, 
    user_update VARCHAR(20) DEFAULT NULL, 
    assignment VARCHAR(15) DEFAULT NULL, 
    id_user_commercial INT(11) NOT NULL, 
    id_group_commercial INT(11) NOT NULL, 
    id_group_scheduling INT(11) DEFAULT NULL, 
    id_group_pilotage INT(11) DEFAULT NULL, 
    PRIMARY KEY (id_folder), 
    INDEX id_product_type (id_product_type) 
) 
ENGINE = INNODB 
AVG_ROW_LENGTH = 1365 
CHARACTER SET latin1 
COLLATE latin1_swedish_ci; 

CREATE schema security; 

CREATE TABLE security.group (grp_id BIGINT AUTO_INCREMENT NOT NULL, grp_name VARCHAR(150) NOT NULL, PRIMARY KEY (grp_id)); 
CREATE TABLE security.user (usr_id BIGINT AUTO_INCREMENT NOT NULL, usr_creation TIMESTAMP, usr_email VARCHAR(128), usr_enabled BOOLEAN NOT NULL, usr_expiration TIMESTAMP, usr_firstname VARCHAR(50), usr_lastname VARCHAR(50), usr_modification TIMESTAMP, usr_password VARCHAR(100) NOT NULL, usr_login VARCHAR(128) NOT NULL, PRIMARY KEY (usr_id)); 

實體Fabricationfolder在戰爭中,包com.sp.sfc.business.model

@Entity 
@Table(name = "fabricationfolder") 
public class Fabricationfolder implements java.io.Serializable { 

    private int idFolder; 

    private ProductType productType; 

    private String folderNumber; 

    private String customerCode; 

    private String customerName; 

    private String brand; 

    private Date creationDate; 

    private String bat; 

    private String grouped; 

    private Date endDate; 

    private String classification; 

    private String observation; 

    private String status; 

    private Double height; 

    private Double width; 

    private boolean flagHeight; 

    private boolean flagWidth; 

    private String intermediateCode; 

    private Date dateUpdate; 

    private String userUpdate; 

    private String assignment; 

    private User userCommercial; 

    private Group groupCommercial; 

    private Group groupScheduling; 

    private Group groupPilotage; 

    private List<Publication> publications = new ArrayList<Publication>(); 

    private List<Message> messages = new ArrayList<Message>(); 

    private List<Element> elements = new ArrayList<Element>(); 

    public Fabricationfolder() { 
    } 

    @Id 
    @Column(name = "id_folder", unique = true, nullable = false) 
    public int getIdFolder() { 
     return this.idFolder; 
    } 

    public void setIdFolder(int idFolder) { 
     this.idFolder = idFolder; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_product_type", nullable = false) 
    public ProductType getProductType() { 
     return this.productType; 
    } 

    public void setProductType(ProductType productType) { 
     this.productType = productType; 
    } 

    @Column(name = "folder_number", nullable = false, length = 15) 
    public String getFolderNumber() { 
     return this.folderNumber; 
    } 

    public void setFolderNumber(String folderNumber) { 
     this.folderNumber = folderNumber; 
    } 

    @Column(name = "customer_code", nullable = false, length = 15) 
    public String getCustomerCode() { 
     return this.customerCode; 
    } 

    public void setCustomerCode(String customerCode) { 
     this.customerCode = customerCode; 
    } 

    @Column(name = "customer_name", nullable = false, length = 32) 
    public String getCustomerName() { 
     return this.customerName; 
    } 

    public void setCustomerName(String customerName) { 
     this.customerName = customerName; 
    } 

    @Column(name = "brand", nullable = false, length = 32) 
    public String getBrand() { 
     return this.brand; 
    } 

    public void setBrand(String brand) { 
     this.brand = brand; 
    } 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "creation_date", nullable = false, length = 19) 
    public Date getCreationDate() { 
     return this.creationDate; 
    } 

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

    @Column(name = "bat", nullable = false, length = 1) 
    public String getBat() { 
     return this.bat; 
    } 

    public void setBat(String bat) { 
     this.bat = bat; 
    } 

    @Column(name = "grouped", nullable = false, length = 1) 
    public String getGrouped() { 
     return this.grouped; 
    } 

    public void setGrouped(String grouped) { 
     this.grouped = grouped; 
    } 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "fabrication_limit_date", nullable = false, length = 19) 
    public Date getEndDate() { 
     return this.endDate; 
    } 

    public void setEndDate(Date fabricationLimitDate) { 
     this.endDate = fabricationLimitDate; 
    } 

    @Column(name = "classification", length = 15) 
    public String getClassification() { 
     return this.classification; 
    } 

    public void setClassification(String classification) { 
     this.classification = classification; 
    } 

    @Column(name = "observation", length = 250) 
    public String getObservation() { 
     return this.observation; 
    } 

    public void setObservation(String observation) { 
     this.observation = observation; 
    } 

    @Column(name = "status", length = 30) 
    public String getStatus() { 
     return this.status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    @Column(name = "height") 
    public Double getHeight() { 
     return this.height; 
    } 

    @Column(name = "height") 
    public void setHeight(Double height) { 
     this.height = height; 
    } 

    @Column(name = "width", precision = 7) 
    public Double getWidth() { 
     return this.width; 
    } 

    public void setWidth(Double width) { 
     this.width = width; 
    } 

    @Column(name = "intermediate_code", length = 15) 
    public String getIntermediateCode() { 
     return this.intermediateCode; 
    } 

    public void setIntermediateCode(String intermediateCode) { 
     this.intermediateCode = intermediateCode; 
    } 

    @Temporal(TemporalType.DATE) 
    @Column(name = "date_update", length = 10) 
    public Date getDateUpdate() { 
     return this.dateUpdate; 
    } 

    public void setDateUpdate(Date dateUpdate) { 
     this.dateUpdate = dateUpdate; 
    } 

    @Column(name = "user_update", length = 20) 
    public String getUserUpdate() { 
     return this.userUpdate; 
    } 

    public void setUserUpdate(String userUpdate) { 
     this.userUpdate = userUpdate; 
    } 

    @Column(name = "assignment", length = 15) 
    public String getAssignment() { 
     return this.assignment; 
    } 

    public void setAssignment(String assignment) { 
     this.assignment = assignment; 
    } 

    @OrderBy("publicationDate ASC") 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "fabricationfolder") 
    public List<Publication> getPublications() { 
     return this.publications; 
    } 

    public void setPublications(List<Publication> publications) { 
     this.publications = publications; 
    } 

    public void setMessages(List<Message> messages) { 
     this.messages = messages; 
    } 

    @OrderBy("creationDate ASC") 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "fabricationfolder") 
    public List<Element> getElements() { 
     return this.elements; 
    } 

    public void setElements(List<Element> elements) { 
     this.elements = elements; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_user_commercial", nullable = true, referencedColumnName= "usr_id") 
    public User getUserCommercial() { 
     return userCommercial; 
    } 

    public void setUserCommercial(User userCommercial) { 
     this.userCommercial = userCommercial; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_group_commercial", nullable = true, referencedColumnName= "grp_id") 
    public Group getGroupCommercial() { 
     return groupCommercial; 
    } 

    public void setGroupCommercial(Group groupCommercial) { 
     this.groupCommercial = groupCommercial; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_group_scheduling", nullable = true, referencedColumnName= "grp_id") 
    public Group getGroupScheduling() { 
     return groupScheduling; 
    } 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "fabricationfolder") 
    public List<Message> getMessages() { 
     return messages; 
    } 

    public void setGroupScheduling(Group groupScheduling) { 
     this.groupScheduling = groupScheduling; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id_group_pilotage", nullable = true, referencedColumnName= "grp_id") 
    public Group getGroupPilotage() { 
     return groupPilotage; 
    } 

    public void setGroupPilotage(Group groupPilotage) { 
     this.groupPilotage = groupPilotage; 
    } 

    @Column(name = "flag_height", nullable = false) 
    public boolean isFlagHeight() { 
     return flagHeight; 
    } 

    public void setFlagHeight(boolean flagHeight) { 
     this.flagHeight = flagHeight; 
    } 

    @Column(name = "flag_width", nullable = false) 
    public boolean isFlagWidth() { 
     return flagWidth; 
    } 

    public void setFlagWidth(boolean flagWidth) { 
     this.flagWidth = flagWidth; 
    } 
} 

用戶和組擴展:jar包融爲一體

@Entity 
public class User extends SPUser { 

    public User(String login, String pass, String email) { 
     super(login, pass, email); 
    } 

    public List<Group> getUserGroups(){ 
     List<Group> listGroup = new ArrayList<Group>(); 
     for(SPGroup agroup: getGroups()){ 
      listGroup.add((Group)agroup); 
     } 
     return listGroup; 
    } 
} 

@Entity 
public class Group extends SPGroup { 

    public Group(String groupName) { 
     super(groupName); 
    }  

} 

實體用戶和組。 sfc.security.metier.impl:

@Entity 
@Table(name="group", schema="security") 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
public class SPGroup implements Group { 

    @Id 
    @GeneratedValue 
    @Column(name = "grp_id", unique = true, nullable = false) 
    private Long id; 

    @Column(name = "grp_name", unique = true, nullable = false, length=150) 
    private String name; 

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinTable(name = "group_user", schema="security", joinColumns = { 
      @JoinColumn(name = "grp_id", nullable = false, updatable = false) }, 
      inverseJoinColumns = { @JoinColumn(name = "usr_id", 
        nullable = false, updatable = false) }) 
    private List<SPUser> users = new ArrayList<SPUser>(); 

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "childGroups") 
    private List<SPGroup> parentGroups = new ArrayList<SPGroup>(); 
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinTable(name = "group_group", schema="security", joinColumns = { 
      @JoinColumn(name = "parent_id", nullable = false, updatable = false, referencedColumnName="grp_id") }, 
      inverseJoinColumns = { @JoinColumn(name = "child_id", 
        nullable = false, updatable = false, referencedColumnName="grp_id") }) 
    private List<SPGroup> childGroups = new ArrayList<SPGroup>(); 

    @OneToMany(fetch = FetchType.LAZY, targetEntity=RoleTag.class ,mappedBy = "group", cascade={CascadeType.ALL}) 
    private List<RoleTag> roleTags = new ArrayList<RoleTag>(); 
    @OneToMany(fetch = FetchType.LAZY, targetEntity=PermissionTag.class, mappedBy = "group", cascade={CascadeType.ALL}) 
    private List<PermissionTag> permissionTags = new ArrayList<PermissionTag>(); 

    ... 
} 

@[email protected] 
@EntityListeners(AuditingEntityListener.class) 
@Table(name="user", schema="security") 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
public class SPUser implements User { 

    private static final long serialVersionUID = 6335411953406117295L; 

    @Id 
    @GeneratedValue 
    @Column(name = "usr_id", unique = true, nullable = false) 
    private Long id; 

    @Column(name = "usr_password", nullable = false, length=100) 
    private String password; 
    @Column(name = "usr_login", unique=true, nullable = false, length=128) 
    private String username; 
    @Column(name = "usr_enabled", nullable = false) 
    private boolean enabled; 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "usr_expiration") 
    private Date expirationDate; 

    @Column(name = "usr_firstname", length=50) 
    private String firstname; 
    @Column(name = "usr_lastname", length=50) 
    private String lastname; 
    @Column(name = "usr_email", unique=true, length=128) 
    private String email; 

    @CreatedDate 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "usr_creation") 
    private Date creationDate; 

    @LastModifiedDate 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "usr_modification") 
    private Date modificationDate; 

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "users") 
    private List<SPGroup> groups; 

    ... 
} 

而對於戰爭的Hibernate的配置:

@Configuration 
@EnableTransactionManagement 
@ComponentScan("com.sp.sfc.business") 
@PropertySource("classpath:properties/${environnement:prod}/application.properties") 
public class WebBusinessConfig implements TransactionManagementConfigurer { 

    @Bean 
    public DataSource businessDataSource() { 

     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
     dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/sampledb"); 
     dataSource.setUsername("user"); 
     dataSource.setPassword("pass"); 

     return dataSource; 
    } 
    @Bean 
    public LocalSessionFactoryBean businessSessionFactory() { 
     LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); 
     sessionFactoryBean.setDataSource(businessDataSource()); 
     sessionFactoryBean.setPackagesToScan("com.sp.sfc.business.model,com.sfc.security.metier.impl"); 
     sessionFactoryBean.setHibernateProperties(hibProperties()); 

     return sessionFactoryBean; 
    } 

    private Properties hibProperties() { 
     Properties properties = new Properties(); 
     properties.put(hibernate.dialect, "org.hibernate.dialect.MySQLDialect"); 
     properties.put(hibernate.show_sql, "true"); 
     return properties; 
    } 

    @Bean 
    public HibernateTransactionManager hibernateBusinessTransactionManager() { 
     HibernateTransactionManager transactionManager = new HibernateTransactionManager(); 
     transactionManager.setSessionFactory(businessSessionFactory().getObject()); 
     return transactionManager; 
    } 

    @Override 
    public PlatformTransactionManager annotationDrivenTransactionManager() { 
     return hibernateBusinessTransactionManager(); 
    } 
} 

和JAR配置

<tx:annotation-driven transaction-manager="transactionManagersfcSecurity" /> 

    <jpa:repositories base-package="com.sfc.security.repositories" entity-manager-factory-ref="entityManagerFactorysfcSecurity" transaction-manager-ref="transactionManagersfcSecurity"/> 

    <bean id="entityManagerFactorysfcSecurity" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSourcesfcSecurity"/> 
     <property name="packagesToScan" value="com.sfc.security.metier.impl" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" > 
       <property name="showSql" value="false"/>    
      </bean> 
     </property> 
     <property name="jpaPropertyMap" ref="$HibernateConfigurationsfcSecurity"/> 
    </bean> 

    <util:map id="HibernateConfigurationsfcSecurity" > 
     <entry key="hibernate.generateDdl" value="false" /> 
     <entry key="hibernate.databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> 
     <entry key="hibernate.current_session_context_class" value="thread" /> 
    </util:map> 

    <bean id="dataSourcesfcSecurity" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://127.0.0.1:3306/sampledb" /> 
     <property name="username" value="user" /> 
     <property name="password" value="pass" /> 
     <property name="defaultAutoCommit" value="false" /> 
    </bean> 

    <bean id="transactionManagersfcSecurity" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactorysfcSecurity" /> 
    </bean> 

感謝您的幫助

+0

檢查您的軟件包是否正在掃描配置中的實體。 – Shailendra

+0

配置中的掃描軟件包包含良好的軟件包 – FlyIn71

+0

在jar項目中,體系結構使用jpa和entitymanagerfactory,在war項目中使用hibernate sessionfactory。 有沒有矛盾的方法? – FlyIn71

回答

0

錯誤在那裏:

sessionFactoryBean.setPackagesToScan("com.sp.sfc.business.model,com.sfc.security.metier.impl").split(","); 

分裂失蹤。