If you want name to be a unique column, you need to annotate it appropriately.
@Column(nullable=false,length=50,unique=true)
Matt
On Sat, Aug 2, 2008 at 7:53 PM, Alc4man <[EMAIL PROTECTED]> wrote:
>
> Hiya.
>
> When I run my ItemExistsExceptionTest it never fails like it's supposed to,
> even though it's very similar to the UserExistsExceptionTest.
>
> When I try to persist my entity, hibernate will store the same entity even
> though it already exists and newItem.equals(persistedItem) returns true.
>
> So I'm somewhat baffled as to why it does'nt throw an ItemExistsException.
>
> My item model:
> -------------------------------------
> @Entity
> @Table(name = "item")
> public class Item extends BaseObject implements Serializable {
>
> private static final long serialVersionUID = 1L;
>
> @Id
> @GeneratedValue(strategy = GenerationType.AUTO)
> @Column(name = "item_id")
> private Long itemId;
>
> @Column(name = "name", length = 200)
> private String name;
>
> @Column(name = "description", length = 1000)
> private String description;
>
> @Version
> private Integer version;
>
> ..
>
> public Integer getVersion() {
> return version;
> }
>
> public void setVersion(Integer version) {
> this.version = version;
> }
>
> ..
> public void setDescription(String description) {
> this.description = description;
> }
>
> @Override
> public boolean equals(Object o) {
> if (this == o) return true;
> if (!(o instanceof Item)) {
> return false;
> }
>
> final Item item = (Item) o;
>
> return !(name != null ? !name.equals(item.getName()) :
> item.getName() !=
> null);
> }
>
> @Override
> public int hashCode() {
> if (description != null) {
> return description.hashCode();
> } else return 0;
>
> }
>
> @Override
> public String toString() {
> ToStringBuilder sb = new ToStringBuilder(this,
> ToStringStyle.DEFAULT_STYLE)
> .append(": name ", name);
> return sb.toString();
> }
>
> }
>
> ItemDaoHibernate:
> ----------------------------------------
>
> public class ItemDaoHibernate extends GenericDaoHibernate<Item, Long>
> implements ItemDao {
>
> public ItemDaoHibernate() {
> super(Item.class);
> }
>
> public Item saveItem(Item itemToSave) {
> getHibernateTemplate().saveOrUpdate(itemToSave);
> getHibernateTemplate().flush();
> return itemToSave;
> }
> }
>
> ItemManagerImpl:
> --------------------------
> ...
> public Item saveItem(Item item) throws ItemExistsException {
>
> Date now = new Date();
>
> try {
> return dao.saveItem(item);
> } catch (DataIntegrityViolationException e) {
> e.printStackTrace();
> log.warn(e.getMessage());
> throw new ItemExistsException("Item '" +
> item.getName() + "' name '" +
> item.getName() + "' already exists");
> } catch (EntityExistsException e) {
> e.printStackTrace();
> log.warn(e.getMessage());
> throw new ItemExistsException("Item '" +
> item.getName() + "' name '" +
> item.getName() + "' already exists");
> }
>
> }
>
> application-services.xml (Where I have my AOP advisor:
> ------------------------------------------------------
> <aop:config>
> <aop:advisor id="userManagerTx" advice-ref="userManagerTxAdvice"
> pointcut="execution(* *..service.UserManager.*(..))" order="0"/>
> <aop:advisor id="itemManagerTx" advice-ref="itemManagerTxAdvice"
> pointcut="execution(* *..service.ItemManager.*(..))" order="4"/>
> .....
> <tx:advice id="itemManagerTxAdvice">
> <tx:attributes>
> <tx:method name="save*" rollback-for="ItemExistsException"/>
> </tx:attributes>
> </tx:advice>
> ------------------------------------------------------
>
> Anyone who can gimme me a hand here? This is causing all my detached
> entities to be inserted as new entities and is a major headache.
>
> - Al
>
> --
> View this message in context:
> http://www.nabble.com/Test-for-entity-exists-exception-always-fails-tp18795198s2369p18795198.html
> Sent from the AppFuse - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]