我正在使用一个(旧的)现有MSSQL数据库,该数据库使用一组用户定义的数据类型。您可以通过将这些语句应用于MSSQL数据库来重新创建我的情境:
CREATE TYPE [dbo].[T_MyCustomType] FROM [varchar](10) NOT NULL
GO
CREATE TABLE [dbo].[MyTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [dbo].[T_MyCustomType] NULL,
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED ([ID] ASC))
GO
为了访问这个数据库,我在JBoss EAP 6.4.7服务器中使用了hibernate。可以使用以下实体类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "MyTable")
public class CustomTypeTest {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int ID;
@Column(name = "Name")
private String name;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
我意识到@Column(name = "Name)
注释不是必需的,但我添加它只是为了确保使用正确的列,并且在没有此注释的情况下会出现相同的问题。
为了完整性,这是我使用的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="MyDatabase-MSSQL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/mssql/MyDatabase</jta-data-source>
<class>com.example.CustomTypeTest</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.jboss.as.jpa.hibernate4.infinispan.InfinispanRegionFactory"/>
<property name="hibernate.cache.infinispan.cachemanager" value="java:jboss/infinispan/container/hibernate"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.jdbc.batch_size" value="100"/>
</properties>
</persistence-unit>
</persistence>
当我启动JBoss服务器时,出现以下错误:
javax.persistence.PersistenceException: [PersistenceUnit: main-db-MSSql] Unable to build EntityManagerFactory
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:103)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_65]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_65]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_65]
at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.2.Final-redhat-1.jar:2.1.2.Final-redhat-1]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit:main-db-MSSql]] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:925)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:200)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.access$600(PersistenceUnitServiceImpl.java:57)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:99)
... 4 more
Caused by: org.hibernate.HibernateException: Missing column: Name in MyDatabase.dbo.MyTable
at org.hibernate.mapping.Table.validateColumns(Table.java:366)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1307)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:175)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:514)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1854)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
... 9 more
如果我要将Name
中MyTable
列的数据类型更改为标准varchar(10)
,则可行。但是,现有数据库无法实现这一点。
有没有办法在hibernate中映射MSSQL用户定义的数据类型,以便显示的设置有效?
答案 0 :(得分:0)
您可能只需要@ Column#columnDefinition注释。
或者您需要为这些自定义类型(或两者)的数据库中的用户/角色授予查看定义。