These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: one-to-one mapping problem
PostPosted: Fri Jan 22, 2010 4:34 am 
Beginner
Beginner

Joined: Mon Feb 02, 2009 3:22 am
Posts: 26
Hi all,

I'm using Struts1.2+Spring 2.0+Hibernate3.0.

I have 3 MySQL tables: tb_box, tb_user, tb_match. and one user can only have one box, and one box can only be assigned to one user. The tb_match table reveals the match relationship.

create table tb_box
(
boxlabel varchar(50) not null,
publicip varchar(15),
privateip varchar(15),
timestamp datetime,
status varchar(1),
primary key (boxlabel)
)ENGINE=InnoDB DEFAULT CHARSET=gbk ROW_FORMAT=REDUNDANT;

create table tb_user
(
username varchar(50) not null,
password varchar(50),
company varchar(50),
contact varchar(50),
email varchar(80),
officenumber varchar(50),
mobile varchar(20),
address varchar(100),
zipcode varchar(6),
primary key (username)
)ENGINE=InnoDB DEFAULT CHARSET=gbk ROW_FORMAT=REDUNDANT;

create table tb_match
(
id bigint(20) not null auto_increment,
username varchar(50),
boxlabel varchar(50),
primary key (id),
unique key AK_Key_2 (username),
unique key AK_Key_3 (boxlabel),
constraint FK_Reference_1 foreign key (username)
references tb_user (username) on delete cascade on update cascade,
constraint FK_Reference_2 foreign key (boxlabel)
references tb_box (boxlabel) on delete cascade on update cascade
)ENGINE=InnoDB DEFAULT CHARSET=gbk ROW_FORMAT=REDUNDANT;


The mapping file is following:
-----------------------------------------------------------------------------------------
Tbuser.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hq.ictbox.hi.mapping.TbUser" table="tb_user" catalog="db_ictbox">
<id name="username" type="java.lang.String">
<column name="username" length="50" />
<generator class="assigned"></generator>
</id>
<property name="password" type="java.lang.String">
<column name="password" length="50" />
</property>
<property name="company" type="java.lang.String">
<column name="company" length="50" />
</property>
<property name="contact" type="java.lang.String">
<column name="contact" length="50" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="80" />
</property>
<property name="officenumber" type="java.lang.String">
<column name="officenumber" length="50" />
</property>
<property name="mobile" type="java.lang.String">
<column name="mobile" length="20" />
</property>
<property name="address" type="java.lang.String">
<column name="address" length="100" />
</property>
<property name="zipcode" type="java.lang.String">
<column name="zipcode" length="6" />
</property>
<one-to-one name="tbMatch" class="com.hq.ictbox.hi.mapping.TbMatch" cascade="all" />
</class>
</hibernate-mapping>

-----------------------------------------------------------------------------------------
TbBox.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hq.ictbox.hi.mapping.TbBox" table="tb_box" catalog="db_ictbox">
<id name="boxlabel" type="java.lang.String">
<column name="boxlabel" length="50" />
<generator class="assigned"></generator>
</id>
<version name="timestamp" type="java.util.Date">
<column name="timestamp" length="0" />
</version>
<property name="publicip" type="java.lang.String">
<column name="publicip" length="15" />
</property>
<property name="privateip" type="java.lang.String">
<column name="privateip" length="15" />
</property>
<property name="status" type="java.lang.String">
<column name="status" length="1" />
</property>

<one-to-one name="tbMatch" class="com.hq.ictbox.hi.mapping.TbMatch" cascade="all" />
</class>
</hibernate-mapping>

-----------------------------------------------------------------------------------------
TbMatch.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hq.ictbox.hi.mapping.TbMatch" table="tb_match" catalog="db_ictbox">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="increment"></generator>
</id>
<one-to-one name="tbUser" class="com.hq.ictbox.hi.mapping.TbUser" constrained="true" />
<one-to-one name="tbBox" class="com.hq.ictbox.hi.mapping.TbBox" constrained="true" />

</class>
</hibernate-mapping>

------------------------------------------------------------------------------------
TbUser.java

package com.hq.ictbox.hi.mapping;
// default package

/**
* TbUser entity. @author MyEclipse Persistence Tools
*/

public class TbUser implements java.io.Serializable {


// Fields

private String username;
private String password;
private String company;
private String contact;
private String email;
private String officenumber;
private String mobile;
private String address;
private String zipcode;
private TbMatch tbMatch;


// Constructors

/** default constructor */
public TbUser() {
}

/** minimal constructor */
public TbUser(String username) {
this.username = username;
}

/** full constructor */
public TbUser(String username, String password, String company, String contact, String email, String officenumber, String mobile, String address, String zipcode, TbMatch tbMatch) {
this.username = username;
this.password = password;
this.company = company;
this.contact = contact;
this.email = email;
this.officenumber = officenumber;
this.mobile = mobile;
this.address = address;
this.zipcode = zipcode;
this.tbMatch = tbMatch;
}


// Property accessors

public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

public String getCompany() {
return this.company;
}

public void setCompany(String company) {
this.company = company;
}

public String getContact() {
return this.contact;
}

public void setContact(String contact) {
this.contact = contact;
}

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

public String getOfficenumber() {
return this.officenumber;
}

public void setOfficenumber(String officenumber) {
this.officenumber = officenumber;
}

public String getMobile() {
return this.mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public String getAddress() {
return this.address;
}

public void setAddress(String address) {
this.address = address;
}

public String getZipcode() {
return this.zipcode;
}

public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}

public TbMatch getTbMatch() {
return this.tbMatch;
}

public void setTbMatch(TbMatch tbMatch) {
this.tbMatch = tbMatch;
}


public String toString()
{
StringBuffer bf = new StringBuffer();
bf.append(username).append(", ").append(password);
return bf.toString();
}
}

------------------------------------------------------------------------------------
TbBox.java


package com.hq.ictbox.hi.mapping;
// default package

import java.util.Date;

/**
* TbBox entity. @author MyEclipse Persistence Tools
*/

public class TbBox implements java.io.Serializable {


// Fields

private String boxlabel;
private Date timestamp;
private String publicip;
private String privateip;
private String status;
private TbMatch tbMatch ;


// Constructors

/** default constructor */
public TbBox() {
}

/** minimal constructor */
public TbBox(String boxlabel) {
this.boxlabel = boxlabel;
}

/** full constructor */
public TbBox(String boxlabel, String publicip, String privateip, String status, TbMatch tbMatch) {
this.boxlabel = boxlabel;
this.publicip = publicip;
this.privateip = privateip;
this.status = status;
this.tbMatch = tbMatch;
}


// Property accessors

public String getBoxlabel() {
return this.boxlabel;
}

public void setBoxlabel(String boxlabel) {
this.boxlabel = boxlabel;
}

public Date getTimestamp() {
return this.timestamp;
}

public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

public String getPublicip() {
return this.publicip;
}

public void setPublicip(String publicip) {
this.publicip = publicip;
}

public String getPrivateip() {
return this.privateip;
}

public void setPrivateip(String privateip) {
this.privateip = privateip;
}

public String getStatus() {
return this.status;
}

public void setStatus(String status) {
this.status = status;
}

public TbMatch getTbMatch() {
return this.tbMatch;
}

public void setTbMatch(TbMatch tbMatch) {
this.tbMatch = tbMatch;
}

public String toString()
{
StringBuffer bf = new StringBuffer();
bf.append(boxlabel).append(", ").append(publicip).append(", ").append(privateip);
return bf.toString();
}

}

------------------------------------------------------------------------------------
TbMatch.java

package com.hq.ictbox.hi.mapping;
// default package



/**
* TbMatch entity. @author MyEclipse Persistence Tools
*/

public class TbMatch implements java.io.Serializable {


// Fields

private Long id;
private TbUser tbUser;
private TbBox tbBox;


// Constructors

/** default constructor */
public TbMatch() {
}


/** full constructor */
public TbMatch(TbUser tbUser, TbBox tbBox) {
this.tbUser = tbUser;
this.tbBox = tbBox;
}


// Property accessors

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public TbUser getTbUser() {
return this.tbUser;
}

public void setTbUser(TbUser tbUser) {
this.tbUser = tbUser;
}

public TbBox getTbBox() {
return this.tbBox;
}

public void setTbBox(TbBox tbBox) {
this.tbBox = tbBox;
}


public String toString()
{
StringBuffer bf = new StringBuffer();
bf.append(tbUser.getUsername()).append(", " ).append(tbBox.getBoxlabel());
return bf.toString();

}
}


-------------------------------------------------------------------------------------
TbMatchDAO.java:

package com.hq.ictbox.hi.dao;
// default package

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hq.ictbox.hi.mapping.*;
/**
* A data access object (DAO) providing persistence and search support for TbMatch entities.
* Transaction control of the save(), update() and delete() operations
can directly support Spring container-managed transactions or they can be augmented to handle user-managed Spring transactions.
Each of these methods provides additional information for how to configure it for the desired type of transaction control.
* @see .TbMatch
* @author MyEclipse Persistence Tools
*/

public class TbMatchDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(TbMatchDAO.class);
//property constants



protected void initDao() {
//do nothing
}

public void save(TbMatch transientInstance) {
log.debug("saving TbMatch instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(TbMatch persistentInstance) {
log.debug("deleting TbMatch instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public TbMatch findById( java.lang.Long id) {
log.debug("getting TbMatch instance with id: " + id);
try {
// TbMatch instance = (TbMatch) getHibernateTemplate().get("TbMatch", id);
TbMatch instance = (TbMatch) getHibernateTemplate().get("com.hq.ictbox.hi.mapping.TbMatch", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


public List findByExample(TbMatch instance) {
log.debug("finding TbMatch instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding TbMatch instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from TbMatch as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}


public List findAll() {
log.debug("finding all TbMatch instances");
try {
String queryString = "from TbMatch";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public TbMatch merge(TbMatch detachedInstance) {
log.debug("merging TbMatch instance");
try {
TbMatch result = (TbMatch) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(TbMatch instance) {
log.debug("attaching dirty TbMatch instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(TbMatch instance) {
log.debug("attaching clean TbMatch instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static TbMatchDAO getFromApplicationContext(ApplicationContext ctx) {
return (TbMatchDAO) ctx.getBean("TbMatchDAO");
}
}


----------------------------------------------------------------------------------------------

AT last, I have a TbUser object user (username is "xxx" and password is "xxx" ), and I need to find the match from tb_match table,

List list = tbMatchDAO.findByProperty("tbUser", user);

I got error:
org.springframework.orm.hibernate3.HibernateQueryException: Expected positional parameter count: 1, actual parameters: [xxx, xxx] [from TbMatch as model where model.tbUser= ?]; nested exception is org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [xxx, xxx] [from TbMatch as model where model.tbUser= ?]
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:639)


I am not sure if my one-to-one mapping is correct. waht does "Expected positional parameter count: 1" mean?
Please help me ! Thanks in advance.

Yhqian99


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.