Unkown column x in field list - java

I'm trying to create a one to many relation between two tables. TestCase has a FK called Type
Basically many testCases can have one type but a testCase can only have one type.
What am I missing here?
TestCase table
TestType table
But I'm getting
Unkown column idtestType in field list
Model
public class TestCase {
private int id;
private String name;
private TestType type;
private byte[] data;
private String creationDate;
private String createdBy;
public TestCase() {
}
public TestCase(String name, TestType type, byte[] data, String creationDate, String createdBy) {
this.name = name;
this.type = type;
this.data = data;
this.creationDate = creationDate;
this.createdBy = createdBy;
}
TestCase.xml
<hibernate-mapping>
<class name="com.atp.Model.TestCases.TestCase" table="testCases">
<meta attribute="class-description">
This class contains the testCases details.
</meta>
<id name="id" type="int" column="idtestCase">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<many-to-one name="type" class="com.atp.Model.TestCases.TestType" fetch="select">
<column name="idtestType"/>
</many-to-one>
<property name="data" column="data" type="binary"/>
<property name="creationDate" column="creationDate" type="string"/>
<property name="createdBy" column="createdBy" type="string"/>
</class>
</hibernate-mapping>
Model
public class TestType {
private int id;
private String desc;
private Set<TestCase> testCases = new HashSet<>(0);
public TestType() {
}
public TestType(String desc, Set<TestCase> testCases) {
this.desc = desc;
this.testCases = testCases;
}
TestType.xml
<hibernate-mapping>
<class name="com.atp.Model.TestCases.TestType" table="testType">
<meta attribute="class-description">
This class contains the testCases details.
</meta>
<id name="id" type="int" column="idtestType">
<generator class="native"/>
</id>
<property name="desc" column="desc" type="string"/>
<set name="testCases">
<key>
<column name="idtestType" />
</key>
<one-to-many class="com.atp.Model.TestCases.TestCase" />
</set>
</class>
</hibernate-mapping>
Trying to add it to the database
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);
TestType testType = new TestType();
testType.setDesc(type);
try {
data = file.getBytes();
TestCase testCase = new TestCase(testName,testType,data,creationDate,createdBy);
testType.getTestCases().add(testCase);
Database.addToDatabase(testCase);
} catch (IOException e) {
e.printStackTrace();
}
}

You're missing a
idtestType
column in the TestCase table.
Or, if you want to use the type column in the existing table your TestCase.xml-Mapping is wrong and should be
<many-to-one name="type" class="com.atp.Model.TestCases.TestType" fetch="select">
<column name="type"/>
</many-to-one>
(column is "type" instead of "idtestType")

Related

Is it required to create a DTO for UI for composite identifier class also in Hibernate?

My entity classes:
Order.java
public class Order {
private int id;
private Set<OrderLine> lines = new HashSet<OrderLine>();
// Setters & Getters
}
OrderLine.java
public class OrderLine {
private OrderLineId id;
private String name;
private Order order;
// Setters & Getters
}
OrderLineId.java
public class OrderLineId implements Serializable{
private int lineId;
private int orderId;
private int customerId;
// Setters & Getters
}
My mapping file:
<hibernate-mapping>
<class name="Order" table="TEST_Order">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<set name="lines" cascade="all">
<key column="orderId"/>
<one-to-many class="OrderLine"/>
</set>
</class>
<class name="OrderLine" table="TEST_OrderLine">
<composite-id name="id" class="OrderLineId">
<key-property name="lineId"/>
<key-property name="orderId"/>
<key-property name="customerId"/>
</composite-id>
<property name="name"/>
<many-to-one name="order" class="Order"
insert="false" update="false">
<column name="orderId"/>
</many-to-one>
</class>
</hibernate-mapping>
I have created Separate DTO's for Order and OrderLine. Do I need to create a separate DTO for OrderLineId (which is a composite Identifier class as mentioned above) as well ?

Hibernate, trying to access grandchild fields one to many relationship

I have the following structure in my database:
I've mapped them in hibernate configuration files like this:
<hibernate-mapping>
<class name="model.CartEntity" table="cart">
<id name="id" type="int" column="id">
<generator class="native" />
</id>
<set name="products" table="cart_product_record"
inverse="true" lazy="false" fetch="select" cascade="save-update, delete">
<key>
<column name="cart_id" not-null="false" />
</key>
<one-to-many class="model.ProductRecordEntity" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="model.ProductRecordEntity" table="product_record">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<many-to-one name="product" class="model.ProductEntity" fetch="select" cascade="save-update, delete">
<column name="product_id" />
</many-to-one>
<many-to-one name="cart" class="model.CartEntity" fetch="select" cascade="save-update, delete">
<column name="cart_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="model.ProductEntity" table="product">
<id name="id" type="int" column="id">
<generator class="native" />
</id>
<set name="productRecords" table="product_record_product" inverse="true"
lazy="false" fetch="select" cascade="save-update, delete">
<key>
<column name="product_id" not-null="true" />
</key>
<one-to-many class="model.ProductRecordEntity" />
</set>
</class>
</hibernate-mapping>
I retrieve from the database the cart which has all the product records, no problem so far, I get all the necessary data correctly.
CartEntity cart = findById(id);
List<ProductRecordEntity> productRecords = cart.getProductRecords();
The problem is when I try to get the product from the product record:
for (ProductRecordEntity productRecord: productRecords){
ProductEntity product = productRecord.getProduct();
}
I get an object with all fields with their default value, not the information from database.
When saving the entities in the database, the ids are populated correctly.
Product record table:
And the product table:
These are the entities:
public class CartEntity {
private int id;
private Set<ProductRecordEntity> products = new HashSet<ProductRecordEntity>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<ProductRecordEntity> getProducts() {
return products;
}
public void setProducts(Set<ProductRecordEntity> products) {
this.products = products;
}
}
public class ProductRecordEntity {
private int id;
private CartEntity cart;
private ProductEntity product;
public CartEntity getCart() {
return cart;
}
public void setCart(CartEntity cart) {
this.cart = cart;
}
public ProductEntity getProduct() {
return product;
}
public void setProduct(ProductEntity product) {
this.product = product;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class ProductEntity implements Serializable{
private int id;
private Set<ProductRecordEntity> productRecords;
public Set<ProductRecordEntity> getProductRecords() {
return productRecords;
}
public void setProductRecords(Set<ProductRecordEntity> productRecords) {
this.productRecords = productRecords;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
This is the order of operations on the database:
persist(cartEntity);
persist(productEntity);
productRecordEntity.setCartEntity(cartEntity);
cartEntity.getProducts().add(productRecordEntity);
productRecordEntity.setProduct(productEntity);
persist(productRecordEntity);
productEntity.getProductRecords().add(productRecordEntity);
update(productEntity);
update(cartEntity);
Any idea what I'm missing? I can't find where the problem is, I've double, triple checked everything.
for CartEntity and ProductEntity mapping, why are you using association tables? you should use table="product_record".
I fixed it by adding lazy="false" to the ProductOrder xml mapping.
<hibernate-mapping>
<class name="model.ProductOrderEntity" table="product_order">
<id name="id" type="int" column="product_order_id">
<generator class="native" />
</id>
<property name="quantity" column="quantity" type="int" />
<property name="price" column="price" type="java.lang.Float" />
<many-to-one name="product" class="model.ProductEntity"
fetch="select" lazy="false">
<column name="product_id" not-null="true" />
</many-to-one>
<many-to-one name="order" class="model.OrderEntity"
fetch="select" lazy="false">
<column name="order_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
I think it's an initialization problem, when getting the product records from the database I should initialize the product field while the session is still opened, like this:
Hibernate.initialize(product);

Hibernate Selecting class property with query

I have a class called product here is the definition
public class Product {
private String productId;
private Set<Label> name;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public Set<Label> getName() {
return name;
}
public void setName(Set<Label> name) {
this.name = name;
}
}
i want the property called name to be retrieved via a select statement so I added this in my mapping file.
<class name="Product" table="PRODUCT">
<id name="productId" type="java.lang.String">
<column name="PRODUCTID" />
<generator class="assigned" />
</id>
<set name="name" cascade="all" inverse="true" lazy="false">
<key column="CONTENTID" />
<one-to-many class="com.dbs.web.models.org.Label" />
<loader query-ref="nameLabel" />
</set>
</class>
<sql-query name="nameLabel">
<load-collection alias="lbl" role="Product.name" />
SELECT {lbl.*} FROM LABEL lbl where lbl.CONTENTID = :productId and
lbl.KEY ='name'
</sql-query>
So all this works well. I am just a little worried that it would not perform very well. Its not like there will be thousands of record return by the sql-query it might be like 10.
Is there another way that this can be achieved.

Hibernate many One To Many relation in one class

i have three entity and Main(User) enitiy is in relation with other two entity,how can i retrive list of three entities from database in one query using hibernate
package hib.test;
import java.util.HashSet;
import java.util.Set;
public class Country {
private Integer id;
private String country;
private Set<User> userList = new HashSet<User>();
public Country() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Set<User> getUserList() {
return userList;
}
public void setUserList(Set<User> userList) {
this.userList = userList;
}
}
User.java
package hib.test;
public class User {
private Integer id;
private UserType userType;
private Country country;
public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public UserType getUserType() {
return userType;
}
public void setUserType(UserType userType) {
this.userType = userType;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
}
UserType.java
package hib.test;
import java.util.HashSet;
import java.util.Set;
public class UserType {
private Integer id;
private String userType;
private Set<User> userList = new HashSet<User>();
public UserType() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public Set<User> getUserList() {
return userList;
}
public void setUserList(Set<User> userList) {
this.userList = userList;
}
}
country.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="hib.test.Country" table="COUNTRY">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="country" type="java.lang.String">
<column name="COUNTRY" />
</property>
<set name="userList" table="USER" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="hib.test.User" />
</set>
</class>
</hibernate-mapping>
user.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="hib.test.User" table="USER">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="userType" class="hib.test.UserType" fetch="join">
<column name="USERTYPE" />
</many-to-one>
<many-to-one name="country" class="hib.test.Country" fetch="join">
<column name="COUNTRY" />
</many-to-one>
</class>
</hibernate-mapping>
usertype.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="hib.test.UserType" table="USERTYPE">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="userType" type="java.lang.String">
<column name="USERTYPE" />
</property>
<set name="userList" table="USER" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="hib.test.User" />
</set>
</class>
</hibernate-mapping>
How can i retrive List<User>, List<Country> and List<UserType> with one query
EDIT
public static List<UserType> getUserTypeList() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
List<UserType> list = null;
try {
transaction = session.beginTransaction();
list = session.createQuery("from UserType as u").list();
if (list != null) {
for (UserType uType : list)
Hibernate.initialize(uType.getUserList());
}
transaction.commit();
} catch (Exception e) {
if (transaction != null)
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return list;
}
Actually I have 1 JTable and Two combo box each for UserType and Country
So when i select any data in combo box JTable data should be filter according to selected value and in memory it shoud be save selected UserType and Country object.
if you want relational data at once and memory is not an issue, do lazy="false"
try defining fetch type to EAGER on you user mapping, that way when you load your user it will load the country and userType.
You need three queries to do that:
select u from User u;
select ut from UserType ut;
select c from Country c;
EDIT:
If what you actually want is a list of all the user types, with the users of each user type, and the country of each user of each user type, all this loaded in a single query, then you need fetch joins, as explained in the Hibernate documentation:
select userType from UserType userType
left join fetch userType.users user
left join fetch user.country

hibernate creates problem for table with composite primary key while generating hibernate classes with eclipse

I have 2 tables as given below.
feedback_answer
question_id number(primary key)
answer_id number (primary key)
answer_en varchar2(255)
answer_it varchar2(255)
user_feedback
verification_id number(primary key)
question_id number(foreign key - feedback_answer)
answer_id number(foreign key - feedback_answer)
I am getting following error for hibernate after hibernate class generation in eclipse
ERROR ::
2009-12-29 14:57:34,093 ERROR java.lang.Class (http-8080-2) - Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FK39E53D791A62BD16:USER_FEEDBACK [QUESTION_ID])) must have same number of columns as the referenced primary key (FEEDBACK_ANSWER [QUESTION_ID,ANSWER_ID])
Following is the java code for feedback_answer
public class FeedbackAnswer implements java.io.Serializable {
private FeedbackAnswerId id;
private FeedbackQuestion feedbackQuestion;
private String answerEn;
private String answerIt;
private Set<Verification> verifications = new HashSet<Verification>(0);
public FeedbackAnswer() {
}
public FeedbackAnswer(FeedbackAnswerId id, FeedbackQuestion feedbackQuestion) {
this.id = id;
this.feedbackQuestion = feedbackQuestion;
}
public FeedbackAnswer(FeedbackAnswerId id,
FeedbackQuestion feedbackQuestion, String answerEn,
String answerIt, Set<Verification> verifications) {
this.id = id;
this.feedbackQuestion = feedbackQuestion;
this.answerEn = answerEn;
this.answerIt = answerIt;
this.verifications = verifications;
}
public FeedbackAnswerId getId() {
return this.id;
}
public void setId(FeedbackAnswerId id) {
this.id = id;
}
public FeedbackQuestion getFeedbackQuestion() {
return this.feedbackQuestion;
}
public void setFeedbackQuestion(FeedbackQuestion feedbackQuestion) {
this.feedbackQuestion = feedbackQuestion;
}
public String getAnswerEn() {
return this.answerEn;
}
public void setAnswerEn(String answerEn) {
this.answerEn = answerEn;
}
public String getAnswerIt() {
return this.answerIt;
}
public void setAnswerIt(String answerIt) {
this.answerIt = answerIt;
}
public Set<Verification> getVerifications() {
return this.verifications;
}
public void setVerifications(Set<Verification> verifications) {
this.verifications = verifications;
}
}
Following is the hibernate code for feedback_answer
<hibernate-mapping>
<class name="com.certilogo.inspect.backend.FeedbackAnswer" table="FEEDBACK_ANSWER" schema="SCOTT">
<composite-id name="id" class="com.certilogo.inspect.backend.FeedbackAnswerId">
<key-property name="answerId" type="long">
<column name="ANSWER_ID" precision="22" scale="0" />
</key-property>
<key-property name="questionId" type="long">
<column name="QUESTION_ID" precision="22" scale="0" />
</key-property>
</composite-id>
<many-to-one name="feedbackQuestion" class="com.certilogo.inspect.backend.FeedbackQuestion" update="false" insert="false" fetch="select">
<column name="QUESTION_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<property name="answerEn" type="string">
<column name="ANSWER_EN" />
</property>
<property name="answerIt" type="string">
<column name="ANSWER_IT" />
</property>
<set name="verifications" inverse="true" table="USER_FEEDBACK">
<key>
<column name="ANSWER_ID" precision="22" scale="0" />
<column name="QUESTION_ID" precision="22" scale="0" />
</key>
<many-to-many entity-name="com.certilogo.inspect.backend.Verification">
<column name="VERIFICATION_ID" precision="22" scale="0" not-null="true" unique="true" />
</many-to-many>
</set>
</class>
Can anyone help me regarding the matter?
Thanks.
amar4kintu
to solve above problem.. I changed my table structure to take one primary key only.. and it solved my problem.. I had to do that because I do not have much time as I need to complete work in time...
so thanks to all who tried to help me out here..

Categories