Hibernate一对多关联报错:Unknown column idtestType in field list
Alright, let's break down exactly what's going wrong here and get your TestCase <-> TestType one-to-many association working properly.
1. The Core Issue: Missing Database Column
That error message is telling you straight up: your testCases database table doesn't have an idtestType column. Hibernate needs this column to store the foreign key that links each TestCase to its corresponding TestType.
Fix:
- Option 1 (Manual): Run an ALTER TABLE command to add the column to your
testCasestable:
(If you want to enforce referential integrity, add a foreign key constraint pointing toALTER TABLE testCases ADD COLUMN idtestType INT;testType.idtestTypetoo.) - Option 2 (Automatic): If you're still in development, set
hibernate.hbm2ddl.auto = updatein your Hibernate configuration. This will let Hibernate automatically create or update your table structures to match your mappings.
2. Broken Persistence Order in Your Add Method
Right now, you're creating a new TestType object and immediately linking it to a TestCase without saving the TestType first. Hibernate can't generate a primary key (idtestType) for the TestType until it's persisted to the database—so when it tries to save the TestCase, it has no value to put in the idtestType column.
Fixed addTestCase Method:
public static void addTestCase(String testName, String type, MultipartFile file, String createdBy) { byte[] data; Date date = new Date(); final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); String creationDate = sdf.format(date); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // First, check if the TestType already exists to avoid duplicates TestType testType = (TestType) session.createQuery("from TestType where desc = :type") .setParameter("type", type) .uniqueResult(); // If it doesn't exist, create and save it if (testType == null) { testType = new TestType(); testType.setDesc(type); session.save(testType); // This generates the idtestType value } data = file.getBytes(); TestCase testCase = new TestCase(testName, testType, data, creationDate, createdBy); testType.getTestCases().add(testCase); session.save(testCase); tx.commit(); } catch (IOException | HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); } }
3. Optional: Optimize Your Mapping Configuration
Your current mappings are mostly correct, but adding one small tweak will make Hibernate handle the association more efficiently:
- In your
TestType.hbm.xml, addinverse="true"to the<set>mapping. This tells Hibernate that the TestCase side (the "many" end) is responsible for maintaining the association, which prevents unnecessary SQL statements:
<set name="testCases" inverse="true"> <key> <column name="idtestType" /> </key> <one-to-many class="com.atp.Model.TestCases.TestCase" /> </set>
Wrap-Up
- First, make sure the
idtestTypecolumn exists in yourtestCasestable. - Fix your persistence logic to save the TestType before linking it to a TestCase.
- Optional: Add
inverse="true"to your TestType set mapping for better performance.
That should resolve the "Unknown column" error and get your one-to-many association working as expected.
内容的提问来源于stack exchange,提问作者user9176898

