我正在使用Spring mvc Web应用程序,我需要使用Hibernate创建一个城镇.以下是我的城镇模型.
@Entity
@Table(name="town")
public class TownModel {
@Id
@Column(name="townid")
@GeneratedValue
private Integer townId;
@Column(name="name")
private String townName;
@Column(name="desc")
private String townDesc;
@ManyToOne(optional = true)
@JoinColumn(name="districtid")
private DistrictModel districtModel;
}
Run Code Online (Sandbox Code Playgroud)
我有另一个名为district的实体.城镇是区的一部分,一个区可以有多个城镇.以下是我的区域模型:
@Entity
@Table(name="district")
public class DistrictModel {
@Id
@Column(name="districtid")
@GeneratedValue
private Integer districtId;
@Column(name="name")
private String name;
@Column(name="desc")
private String description;
@OneToMany(mappedBy = "districtModel", fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<TownModel> townModelList;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下Hibernate代码来保存城镇:
Session session = sessionFactory.getCurrentSession();
session.save(townModel);
Run Code Online (Sandbox Code Playgroud)
但它显示错误:
21:22:08,104 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--127.0.0.1-9090-1) SQL Error: 1064, SQLState: …Run Code Online (Sandbox Code Playgroud) 我正在使用 Hibernate 开发 Spring MVC 应用程序。以下是我的调度程序 servlet 代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.example.abc" />
<tx:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/MySqlDS" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="jndiDataSource" />
<property name="packagesToScan" value="com.example.abc"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我没有任何 Hibernate 配置文件,因为 Hibernate 映射是在模型类中完成的。我需要打印从 Hibernate …
我有一个表,我需要在现有列上添加外键.以下是创建表:
CREATE TABLE `itemtx` (
`itemTxid` int(11) NOT NULL AUTO_INCREMENT,
`itemcode` varchar(1) NOT NULL,
`weight` decimal(7,3) DEFAULT NULL,
`txtype` varchar(10) DEFAULT 'Pickup',
`tripstopid` int(11) NOT NULL,
`barcode` varchar(25) DEFAULT NULL,
`bagcount` int(11) DEFAULT '1',
PRIMARY KEY (`itemTxid`)
) ENGINE=InnoDB AUTO_INCREMENT=33524 DEFAULT CHARSET=latin1
Run Code Online (Sandbox Code Playgroud)
我需要在tripstopid列上添加外键.我不能删除或清空表,因为它包含数据.以下是引用的表:
CREATE TABLE `tripstop` (
`tripstopid` int(11) NOT NULL AUTO_INCREMENT,
`tripid` int(11) DEFAULT NULL,
`locationName` varchar(100) DEFAULT NULL,
`userName` varchar(100) DEFAULT NULL,
`locationid` int(11) DEFAULT NULL,
`datetime` varchar(20) DEFAULT NULL,
`createts` varchar(20) DEFAULT NULL,
`latitude` double DEFAULT NULL, …Run Code Online (Sandbox Code Playgroud)