-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problem: mapping? id?
PostPosted: Sat May 15, 2004 7:49 am 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
Hibernate version 2.1.3
MySQL database

I have the following situation:
A user class, having a member constraint.
A user has one constraint, but a constraint has many users. The constraint member determines, for what user, a user is responsible.

I have the following output, and error message.

Is it an 'id' problem?

Code:
Constraints of User1:
User: user2
User: user3
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: User.constraints
   at net.sf.hibernate.impl.SessionImpl.checkNullability(SessionImpl.java:1269)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:921)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:850)


Thanks in advance...
User class
Code:
public class User {
    private Long id;
    private String name;
    private Constraints constraints=new Constraints();
   
    public User() {
       
    }
   
    public User(String name) {
        this.name=name;
    }
   
    public Constraints getConstraints() {
        return constraints;
    }
    public void setConstraints(Constraints constraints) {
        this.constraints = constraints;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
   
    public String toString() {
        return "User: "+name;
    }
}


Constraint class
Code:
import java.util.ArrayList;
import java.util.List;

public class Constraints {
    private Long id;
    private List excludedUsers=null;
   
    public void addUser(User user) {
        if (excludedUsers==null) excludedUsers=new ArrayList();
        excludedUsers.add(user);
    }
   
    public List getExcludedUsers() {
        return excludedUsers;
    }
    public void setExcludedUsers(List excludedUsers) {
        this.excludedUsers = excludedUsers;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}


Main class
Code:
public class Main {
    private SessionFactory sessions;
   
   public void configure() throws HibernateException {
       sessions = new Configuration()
       .addClass(User.class)
      .addClass(Constraints.class)
       .setProperty(Environment.HBM2DDL_AUTO, "create")
       .buildSessionFactory();
   }   
   
   public void Store(User user) throws HibernateException {
        Session session = sessions.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.saveOrUpdate(user);
            tx.commit();
        }
        catch (HibernateException he) {
            if (tx!=null) tx.rollback();
            throw he;
        }
        finally {
            session.close();
        }
   }      

    public static void main(String[] args) throws HibernateException {
        Main m = new Main();
        m.configure();
       
      User u1 = new User("user1");
      User u2 = new User("user2");
      User u3 = new User("user3");

      u1.getConstraints().addUser(u2);
      u1.getConstraints().addUser(u3);
      
      System.out.println("Constraints of User1:");
      System.out.println(u1.getConstraints().getExcludedUsers().get(0).toString());
      System.out.println(u1.getConstraints().getExcludedUsers().get(1).toString());
      
      m.Store(u1);
    }
   
}


User mapping
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="User" table="USER">
        <id
            name="id"
            column="ID">         
            <generator class="native"/>
        </id>

        <property name="name" column="NAAM"/>

        <many-to-one
            name="constraints"
            column="CONSTRAINT"
            class="Constraints"
            not-null="true"/>

    </class>
</hibernate-mapping>


Constraint mapping
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>   
    <class
        name="Constraints"
        table="CONSTRAINTS">
       
        <id
            name="id"
            column="ID">           
            <generator class="native"/>
        </id>   
       
        <bag
            name="excludedUsers">           
            <key column="ID"/>
            <one-to-many class="User"/>         
        </bag>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2004 8:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Either use cascading or save the associated objects manually.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2004 8:25 am 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
If I use cascading, hibernate does not create a table user.
But you can see in the output, that hibernate tries to create a table 'user'.
Is it because I want to create a user who has a many-to-one mapping instead of creating a constraint with a one-to-many mapping.
It is important for me to create a user, with a constraint, and not the reverse (a constraint with users). (see Main class).


Following error occured:

Code:
14:19:02,062 DEBUG SchemaExport:132 - drop table if exists USER
14:19:02,062 DEBUG SchemaExport:132 - drop table if exists CONSTRAINTS
14:19:02,062 DEBUG SchemaExport:149 - create table USER (
   ID BIGINT NOT NULL AUTO_INCREMENT,
   NAAM VARCHAR(255),
   CONSTRAINT BIGINT not null,
   primary key (ID)
)
14:19:02,078 ERROR SchemaExport:154 - Unsuccessful: create table USER (ID BIGINT NOT NULL AUTO_INCREMENT, NAAM VARCHAR(255), CONSTRAINT BIGINT not null, primary key (ID))
14:19:02,078 ERROR SchemaExport:155 - Syntax error or access violation,  message from server: "You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'BIGINT not null, primary key (ID))' at line 1"
14:19:02,078 DEBUG SchemaExport:149 - create table CONSTRAINTS (
   ID BIGINT NOT NULL AUTO_INCREMENT,
   primary key (ID)
)
14:19:02,109 DEBUG SchemaExport:149 - alter table USER add index (CONSTRAINT), add constraint FK27E3CB119101DD foreign key (CONSTRAINT) references CONSTRAINTS (ID)
14:19:02,109 ERROR SchemaExport:154 - Unsuccessful: alter table USER add index (CONSTRAINT), add constraint FK27E3CB119101DD foreign key (CONSTRAINT) references CONSTRAINTS (ID)
14:19:02,109 ERROR SchemaExport:155 - Syntax error or access violation,  message from server: "You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT), add constraint FK27E3CB119101DD foreign key (CONST"
14:19:02,109  INFO SchemaExport:160 - schema export complete
14:19:02,125  INFO DriverManagerConnectionProvider:143 - cleaning up connection pool: jdbc:mysql://127.0.0.1:3306/test3
Constraints of User1:
User: user2
User: user3
Hibernate: insert into CONSTRAINTS values ( )
Hibernate: insert into CONSTRAINTS values ( )
Hibernate: insert into USER (NAAM, CONSTRAINT) values (?, ?)
14:19:02,250  WARN JDBCExceptionReporter:38 - SQL Error: 1064, SQLState: 42000
14:19:02,250 ERROR JDBCExceptionReporter:46 - Syntax error or access violation,  message from server: "You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT) values ('user2', 2)' at line 1"
14:19:02,265  WARN JDBCExceptionReporter:38 - SQL Error: 1064, SQLState: 42000
14:19:02,265 ERROR JDBCExceptionReporter:46 - Syntax error or access violation,  message from server: "You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT) values ('user2', 2)' at line 1"
14:19:02,265 ERROR JDBCExceptionReporter:38 - could not insert: [User]
java.sql.SQLException: Syntax error or access violation,  message from server: "You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT) values ('user2', 2)' at line 1"
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1905)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1109)


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

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.