日期:2014-05-19 浏览次数:20890 次
public class User { private int UserId; private String Username; private String Password; private Date RegisterTime; private Set<BankAccount> BankAccounts = new HashSet<BankAccount>(); } public class BankAccount { private int CardNumber; private String AccountName; private User BankUser; }
<hibernate-mapping> <class name="Model.BankAccount" table="bankaccount"> <id name="CardNumber" type="int" column="CardNumber" > <generator class="assigned"/> </id> <property name="AccountName" column="AccountName" /> <many-to-one name="BankUser" column="UserId" class="Model.User"/> </class> </hibernate-mapping> <hibernate-mapping> <class name="Model.User" table="user"> <id name="UserId" type="int" column="UserId" > <generator class="assigned"/> </id> <property name="Username" column="Username" /> <property name="Password" column="Password" /> <property name="RegisterTime" type="timestamp" column="RegisterTime"/> <set name="BankAccounts" table="bankaccount" inverse="true"> <key> <column name="UserId"/> </key> <one-to-many class="Model.BankAccount" /> </set> </class> </hibernate-mapping>
public void saveUserAndBank(String username, String password, int CardNo, String AccountName) { session.beginTransaction(); User user = new User(); user.setUsername(username); user.setPassword(password); user.setRegisterTime(new Date()); // create bank account BankAccount bankAcc = new BankAccount(); bankAcc.setCardNumber(CardNo); bankAcc.setAccountName(AccountName); session.save(user); session.save(bankAcc); bankAcc.setBankUser(user); user.getBankAccounts().add(bankAcc); session.getTransaction().commit(); }
session.save(user); session.save(bankAcc); bankAcc.setBankUser(user);
------解决方案--------------------
你的事务呢
------解决方案--------------------
你这个有这几个地方的问题:
1,你的命名不规范,属性的开头两个字母应该用小写
2,你的配置也有问题,<set name="BankAccounts" table="bankaccount" inverse="true">
<key>
<column name="UserId"/>