【发布时间】:2013-07-18 04:15:05
【问题描述】:
Status: 感谢您的回答,但没有人回答重要标签描述以反映我给出的简单代码。 (2013 年 7 月 20 日)
我已经阅读了很多教程,但每件事都只是混淆了,或者只是作为一个看起来很小或抽象的特定示例。
我真的无法让某些事情在我的脑海中变得合乎逻辑。可能是我用具体的实际代码来学习的。
问题:谁能展示如何使用 spring 3.x 和 hibernate 4.x 制作以下不完整的代码 work completely。
重要:
我希望,即使在这个简单的例子中, 使 sessionfactory 和数据库查询通过休眠 Service 类(在大型应用程序中建立边界服务类使用许多 DAO 并提交事务一次性)
我忘记了我在哪里读到的,也许是在 spring 文档中 - 但它明确表示,不要将 @Transactional 放在你的 DAO 上:P 所以一般来说,您的服务层是您定义事务边界的地方。 服务方法通常是一大堆东西,如果全部通过,则提交,否则失败并回滚 当然,这可能不是一个顽固的规则,但它是我们构建企业资源规划 Web 核心的方式。 我忘记了我在哪里读到的,也许是在 spring 文档中 - 但它明确表示,不要将 @Transactional 放在你的 DAO 上
例如http://blog.patouchas.net/technology/hibernate-dao-java-tutorial/ 喜欢没有弹簧?
注释缺失或不正确。正确的注释应该是什么。
这些类是否会有特定的 spring.xml(除了通常的)?如果可行,我只想选择注释。正确的注释,如 @component 、服务、存储库、资源、自动装配
这就是我通常获得交易的方式
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.getTransaction().commit();
session.close();
现在春天和冬眠
@Controller
public class UserController {
private IUserService userService;
@RequestMapping("/users")
public String creatUser(){
Users user = new Users();
user.setEmail("myemail@mydomain.com");
user.setName("myname");
userService.creatUser(user);
return "user-creation-result";
}
}
public class UserService implements IUserService{
private IUserDAO userDAO;
public void creatUser(Users user){
//what to do here
//how to call the sessionfactory
//and call it in a way that each call
// gives the same instance
userDAO.creatUser(user);
}
}
public class UserDAO implements IUserDAO{
public void creatUser(Users user){
// what to do here?
}
}
不过,这不会那么重要。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="connection.username">postgres</property>
<property name="connection.password">abc</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="taskmanagsetup.Boards" />
<mapping class="taskmanagsetup.BoardPrivileges" />
<mapping class="taskmanagsetup.Boxes" />
<mapping class="taskmanagsetup.BoxPrivileges" />
<mapping class="taskmanagsetup.Groups" />
<mapping class="taskmanagsetup.Tasks" />
<mapping class="taskmanagsetup.TaskPrivileges" />
<mapping class="taskmanagsetup.Users" />
</session-factory>
</hibernate-configuration>
@Entity
public class Users {
@Id
@GeneratedValue
private long id;
@ManyToMany
private Collection<Groups> groupList = new ArrayList<Groups>();
private String type; // admin / teamlead / normal
private String name;
private String email;
private String password;
@Lob
private String description;
private boolean isEnabled;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the groupdId
*/
/**
* @param groupdId the groupdId to set
*/
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the isEnabled
*/
public boolean isIsEnabled() {
return isEnabled;
}
/**
* @param isEnabled the isEnabled to set
*/
public void setIsEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}
/**
* @return the groupList
*/
public Collection<Groups> getGroupList() {
return groupList;
}
/**
* @param groupList the groupList to set
*/
public void setGroupList(Collection<Groups> groupList) {
this.groupList = groupList;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
}
【问题讨论】:
-
那个代码没有用吗?如果是,什么错误?
-
感谢您的回复。它甚至没有写正确。它怎么会起作用。我的问题是如何以工作方式编写它
标签: java spring hibernate jakarta-ee spring-mvc