我使用Spring Mvc应用程序并尝试使用webapp
作为ORM。提供了应用程序结构,
下面提供了MySQL database ERM
文件夹,
下面提供Maven
,
我使用JNDI
进行依赖关系管理,使用database-context.xml
进行数据库连接。 <?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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan
base-package="mobi.puut.database">
</context:component-scan>
<beans profile="production">
<jee:jndi-lookup jndi-name="jdbc/spring" id="dataSource"
expected-type="javax.sql.DataSource">
</jee:jndi-lookup>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>mobi.puut.entities</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven/>
<bean id="exceptionTranslator"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
</bean>
</beans>
</beans>
文件位于下方,
POJO
User
文件位于实体目录中,由Status
和User
类组成。
下面提供了@Entity
@Table(name = "users")
public class User {
@Id
@NotNull
@Column(name = "id")
@GeneratedValue
private int id;
@NotNull
@NotBlank
@Column(name = "name")
@Size(min = 5, max = 45, message = "Name must be between 5 and 45 characters.")
private String name;
public User() {
}
public User(@Size(min = 5, max = 45, message = "Name must be between 5 and 45 characters.") String name) {
this.name = name;
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
if (getId() != user.getId()) return false;
return getName().equals(user.getName());
}
@Override
public int hashCode() {
int result = getId();
result = 31 * result + getName().hashCode();
return result;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
课程,
Status
下面提供了@Entity
@Table(name = "status")
public class Status {
@Id
@NotNull
@GeneratedValue
@Column(name = "id")
private int id;
@NotNull
@ManyToOne
@JoinColumn(name = "id", insertable = false, updatable = false)
// private int user_id;
private User user;
@NotNull
@Column(name = "balance")
private float balance;
@NotNull
@Column(name = "address")
@Size(min = 5, max = 90, message = "Address must be between 5 and 90 characters.")
private String address;
@NotNull
@Column(name = "transaction")
@Size(min = 5, max = 90, message = "Transaction history must be between 5 and 90 characters.")
private String transaction;
public Status() {
}
public Status(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Status(User user, @NotNull float balance, @NotNull @Size(min = 5, max = 90, message = "Address must be between 5 and 90 characters.") String address) {
this.user = user;
this.balance = balance;
this.address = address;
}
public Status(User user, @NotNull float balance, @NotNull @Size(min = 5, max = 90, message = "Address must be between 5 and 90 characters.") String address, @NotNull @Size(min = 5, max = 90, message = "Transaction history must be between 5 and 90 characters.") String transaction) {
this.user = user;
this.balance = balance;
this.address = address;
this.transaction = transaction;
}
// public Status(@NotNull int user_id) {
// this.user_id = user_id;
// }
public Status(float balance, String transaction, String address, int user_id) {
// this.user_id = user_id;
this.balance = balance;
this.address = address;
this.transaction = transaction;
}
public Status(int id, int user_id, float balance, String address, String transaction) {
super();
this.id = id;
// this.user_id = user_id;
this.balance = balance;
this.address = address;
this.transaction = transaction;
}
public int getId() {
return id;
}
// public int getUser_id() {
// return user_id;
// }
public float getBalance() {
return balance;
}
public String getAddress() {
return address;
}
public String getTransaction() {
return transaction;
}
public void setId(int id) {
this.id = id;
}
// public void setUser_id(int user_id) {
// this.user_id = user_id;
// }
public void setBalance(float balance) {
this.balance = balance;
}
public void setAddress(String address) {
this.address = address;
}
public void setTransaction(String transaction) {
this.transaction = transaction;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Status)) return false;
Status status = (Status) o;
if (getId() != status.getId()) return false;
if (Float.compare(status.getBalance(), getBalance()) != 0) return false;
if (!getUser().equals(status.getUser())) return false;
if (!getAddress().equals(status.getAddress())) return false;
return getTransaction().equals(status.getTransaction());
}
@Override
public int hashCode() {
int result = getId();
result = 31 * result + getUser().hashCode();
result = 31 * result + (getBalance() != +0.0f ? Float.floatToIntBits(getBalance()) : 0);
result = 31 * result + getAddress().hashCode();
result = 31 * result + getTransaction().hashCode();
return result;
}
@Override
public String toString() {
return "Status{" +
"id=" + id +
", user=" + user +
", balance=" + balance +
", address='" + address + '\'' +
", transaction='" + transaction + '\'' +
'}';
}
}
课程,
public class StatusRowMapper implements RowMapper<Status> {
@Override
public Status mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setName(rs.getString("name"));
Status status = new Status();
status.setId(rs.getInt("id"));
status.setBalance(rs.getFloat("balance"));
status.setAddress(rs.getString("address"));
status.setTransaction(rs.getString("transaction"));
status.setUser(user);
return status;
}
}
我编写了用于将状态映射到用户的源代码,
@Repository
@Transactional
@Component("bitcoinWwallet")
public class BitcoinWalletDAO {
@Autowired
private SessionFactory sessionFactory;
public Session session(){
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public List<Status> getAllWalletStatus() {
Criteria crit = session().createCriteria(Status.class);
return crit.list();
}
}
我写的类和方法来获取所有状态,
JSP
当我打电话从控制器获取Type Exception Report
Message Request processing failed; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing
failed; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
中的所有雕像时,我收到错误,
@Transactional
如果我在类BitcoinWalletDAO
的顶部注释掉@ExceptionHandler(DataAccessException.class)
public String handleDatabaseException(DataAccessException ex) {
ex.printStackTrace();
return "error";
}
,我会从错误处理程序中获取数据库连接错误,
SQL
以前,我使用 @SuppressWarnings("unchecked")
public List<Status> getAllWalletStatus() {
final String sqlString = "SELECT * FROM status";
return jdbc.query(sqlString, new RowMapper<Status>() {
public Status mapRow(ResultSet rs, int rowNum) throws SQLException {
Status status = new Status();
status.setId(rs.getInt("id"));
status.setBalance(rs.getFloat("balance"));
status.setTransaction(rs.getString("transaction"));
status.setAddress(rs.getString("address"));
// status.setUser_id(rs.getInt("user_id"));
return status;
}
});
}
来获取数据库中的所有雕像,并且工作正常,
TABLE1.name , TABLE1.description, TABLE2.category.
如果需要,我可以提供更多信息。如何解决这个问题?