我在Java中具有包含3个主键的模型,并且TokensPK
类显示了我的主键。
hbm.xml
文件的标签composite-id
显示了这些PK并显示了2类之间的关系,但我听不懂
public class UserTokens {
private String userId;
private String loginProvider;
private String name;
private String value;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLoginProvider() {
return loginProvider;
}
public void setLoginProvider(String loginProvider) {
this.loginProvider = loginProvider;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
和
public class TokensPK implements Serializable {
private String userId;
private String loginProvider;
private String name;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLoginProvider() {
return loginProvider;
}
public void setLoginProvider(String loginProvider) {
this.loginProvider = loginProvider;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是我的hbm.xml文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="oauth.identity.IdentityModel.UserTokens" table="UserTokens" schema="dbo" catalog="BBB">
<composite-id mapped="true" class="oauth.identity.IdentityModel.TokensPK">
<key-property name="userId">
<column name="UserId" sql-type="nvarchar(450)" length="450"/>
</key-property>
<key-property name="loginProvider">
<column name="LoginProvider" sql-type="nvarchar(450)" length="450"/>
</key-property>
<key-property name="name">
<column name="Name" sql-type="nvarchar(450)" length="450"/>
</key-property>
</composite-id>
<property name="value">
<column name="Value" sql-type="nvarchar(max)" length="2147483647" not-null="true"/>
</property>
</class>
</hibernate-mapping>
和我的ApplicationContext
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://Data Source=localhost;Initial Catalog=DB;Integrated Security=True" />
<property name="username" value="sa" />
<property name="password" value="123" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>UserTokens.hbm.xml</value>
</list>
</property>
</bean>
</beans>
我知道桌子有3个主要桌子,但我不知道我的错在哪里。 当我想用此模型创建数据库时,这给我一个错误,我无法解决它
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [ApplicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: expecting IdClass mapping
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1783) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:404) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1275) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1263) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
at oauth.identity.IdentityApplication.main(IdentityApplication.java:12) ~[classes/:na]
Caused by: java.lang.IllegalArgumentException: expecting IdClass mapping
at org.hibernate.metamodel.internal.AttributeFactory$3.resolveMember(AttributeFactory.java:977) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.metamodel.internal.AttributeFactory$5.resolveMember(AttributeFactory.java:1035) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.metamodel.internal.AttributeFactory.determineAttributeMetadata(AttributeFactory.java:450) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.metamodel.internal.AttributeFactory.buildIdAttribute(AttributeFactory.java:139) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.metamodel.internal.MetadataContext.buildIdClassAttributes(MetadataContext.java:389) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.metamodel.internal.MetadataContext.applyIdMetadata(MetadataContext.java:319) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.metamodel.internal.MetadataContext.wrapUp(MetadataContext.java:222) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:274) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:294) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:615) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:599) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1842) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1779) ~[spring-beans-5.2.0.M2.jar:5.2.0.M2]
... 16 common frames omitted
答案 0 :(得分:0)
如下更新您的实体:
public class UserTokens implements Serializable{
private String userId;
private String loginProvider;
private String name;
private String value;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLoginProvider() {
return loginProvider;
}
public void setLoginProvider(String loginProvider) {
this.loginProvider = loginProvider;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
您的实体类应实现Serializable。另外,在映射文件中替换以下内容
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="oauth.identity.IdentityModel.UserTokens" >
<composite-id >
<key-property name="userId" column="UserId" />
<key-property name="loginProvider" column="LoginProvider" />
<key-property name="name" column="Name" />
</composite-id>
<property name="value">
<column name="Value" />
</property>
</class>
</hibernate-mapping>