我在使用 EntityManager Here my User 实体从数据库中获取数据时遇到问题
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private Integer id;
@Column(name = "username", length = 20, nullable = false)
@JsonProperty
private String username;
@Column(name = "password", nullable = false, unique = true)
@JsonProperty
private String password;
@Column(name = "enabled", nullable = false)
@JsonProperty
private boolean enabled;
@Column(name = "email", nullable = false, unique = true)
@JsonProperty
private String email;
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<UserRole> userRoles;
//getters and setters
Run Code Online (Sandbox Code Playgroud)
我尝试使用用户名搜索用户:
public …Run Code Online (Sandbox Code Playgroud) 我的Spring + Hibernate项目遇到一些问题。当我尝试获取数据时,我有:
HTTP状态500-请求处理失败;嵌套的异常是org.hibernate.HibernateException:如果没有活动的事务,createQuery无效。这是我的xml配置代码:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/users" />
<property name="username" value="root"/>
<property name="password" value="12345"/>
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.springapp.model.User</value>
<value>com.springapp.model.UserRole</value>
<value>com.springapp.model.Project</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
DAO的方法
@Override
public List<User> showAllUsers(){
List<User> users = new ArrayList<User>();
Session session = …Run Code Online (Sandbox Code Playgroud) 我有一个简单的方法接受我的liferay portlet中的PortletResponse和PortletRequest
public void remove(PortletResponse response, PortletRequest request) {
}
Run Code Online (Sandbox Code Playgroud)
我想将响应状态设置为404,就像我可以用HttpServletResponse做的那样 httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST)
你能告诉我怎么做吗?