Status:
回答に感謝しますが、指定された単純なコードを反映して、重要なタグの説明に誰も回答していません。(2013 年 7 月 20 日)
私は多くのチュートリアルを読みましたが、すべてが混同されているか、小さくまたは抽象的に見える特定の例としてのみです.
私は本当に何かを頭の中で論理的にすることができません。私は特定の実際のコードで学ぶかもしれません。
質問: spring 3.x と hibernate 4.xwork completely
で次の不完全なコードを作成する方法を誰か教えてください。
重要:
この単純な例でも、Service クラスで hibernate によるsessionfactoryとデータベースのクエリを作成したいと思います (大規模なアプリケーションのようにそこに境界を作成します Service クラスは多くの DAO を使用し、一度にトランザクションをコミットします)
おそらく春のドキュメントで、どこで読んだか忘れましたが、DAOに@Transactionalを置かないでください:Pしたがって、一般的に、サービス層はトランザクション境界を定義する場所です。通常、サービス メソッドは、すべてが成功した場合はコミットし、それ以外の場合は失敗し、ロールバックする一連のものです。これはもちろん厳格なルールではないかもしれませんが、エンタープライズ リソース プランニングの Web コアを設計した方法です。おそらく春のドキュメントで、どこで読んだか忘れましたが、DAOに@Transactionalを置かないでください
例 http://blog.patouchas.net/technology/hibernate-dao-Java-tutorial/スプリングなしのような?
注釈が欠落しているか、正しくありません。正しい注釈は何ですか。
これらのクラスに固有の spring.xml (通常以外) はありますか? 機能する場合は、注釈のみを優先したいと思います。@component 、service、repository、resource、autowired などの正しいアノテーション
これは私が一般的にトランザクションを取得する方法です
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;
}
}