Hibernate 4中的多个项目和实体:实体未映射
我需要一些帮助来解决一个奇怪的错误:“Fabricationfolder未映射” 以下是上下文: 我有一个项目“war”,其中包括一个库“jar”。它们在两个分离的模式上都有实体。 我不能对jar做任何更改,因为它被其他项目使用。 我使用相同的hibernate版本:4.1.7和Spring4 出于某些原因,我需要将war的一些实体链接到jar实体。为了向jar的实体添加属性,我决定将jar的一些实体扩展到war中。 这是个好办法吗 当我启动web应用程序时,对Fabricationfolder的第一个请求因此错误而失败 以下是配置: 模式: 如果不存在sampledb,则创建数据库 拉丁字符集1 校对拉丁语和瑞典语的ciHibernate 4中的多个项目和实体:实体未映射,hibernate,Hibernate,我需要一些帮助来解决一个奇怪的错误:“Fabricationfolder未映射” 以下是上下文: 我有一个项目“war”,其中包括一个库“jar”。它们在两个分离的模式上都有实体。 我不能对jar做任何更改,因为它被其他项目使用。 我使用相同的hibernate版本:4.1.7和Spring4 出于某些原因,我需要将war的一些实体链接到jar实体。为了向jar的实体添加属性,我决定将jar的一些实体扩展到war中。 这是个好办法吗 当我启动web应用程序时,对Fabricationfolder
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));
war中的实体制造文件夹,包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配置
<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>
感谢您的帮助错误就在那里:
sessionFactoryBean.setPackagesToScan("com.sp.sfc.business.model,com.sfc.security.metier.impl").split(",");
缺少拆分。检查是否正在扫描配置中的实体。配置中的扫描包包含良好的包在jar项目中,体系结构使用jpa和entitymanagerfactory,而在war项目中,它使用hibernate sessionfactory。是否存在冲突?在同一个应用程序中同时使用实体管理器和会话可能不是一个好主意。您的jar配置扫描“com.sfc.security.repositories”包,但您的类“FabricationFolder”位于“com.sp.sfc.business.model”中
@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();
}
}
<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>
sessionFactoryBean.setPackagesToScan("com.sp.sfc.business.model,com.sfc.security.metier.impl").split(",");