Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8972767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:17:46+00:00 2026-06-15T18:17:46+00:00

I just switched from XML mapping to annotations and had to realize that my

  • 0

I just switched from XML mapping to annotations and had to realize that my serialization class does not work any more!
I hope you can help me to find out the reason 🙂

I have a School class that contains an Address

@Entity
@Table(name="schools")
    public class School {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn(name="address_id", nullable=true)
    private Address address;
        ...
}

the Address class:

@Entity
@Table(name="addresses")
public class Address {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name="street", nullable=false, updatable=true)
    private String street;      // with nr
    ...
}

I try to serialize like this:

sessionFactory = MyFactory.getSessionFactory();
            session = sessionFactory.openSession();

            Transaction tx = session.beginTransaction();

            School mbs = new School("interesting school");
            Address a = new Address("garden street 5","12345", "somewhere");
            mbs.setAddress(a);

            session.save(mbs);
            tx.commit();

This worked before with XML, but now doesn’t..

The first Hibernate query action visible on the console is:
Hibernate: insert into schools (address_id, layout_id, name) values (?, ?, ?)

Therefore an error occurs: ERROR: Column ‘address_id’ cannot be null

How can I fix this?

.

Those are the tables:

CREATE TABLE schools(
    id INTEGER NOT NULL AUTO_INCREMENT,
    name CHAR(50),

    address_id INTEGER NOT NULL,
    layout_id INTEGER NOT NULL,

    CONSTRAINT fk_address FOREIGN KEY (address_id) REFERENCES addresses(id),
    CONSTRAINT fk_layout FOREIGN KEY (layout_id) REFERENCES layout_headers(id),

    PRIMARY KEY (id)
);
CREATE TABLE addresses(
    id INTEGER NOT NULL AUTO_INCREMENT,
    street CHAR(55),
    zip CHAR(6),
    city CHAR(60),

    CONSTRAINT addr_pk PRIMARY KEY (id)
);

In my hibernte.cfg.xml I have the following:

<hibernate-configuration>
    <session-factory>
     ...
        <mapping class="creator.models.school.Address" />
        <mapping class="creator.models.school.Report" />
        <mapping class="creator.models.school.School" />
        <mapping class="creator.models.school.SchoolClass" />
        ...
    </session-factory>
</hibernate-configuration>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:17:48+00:00Added an answer on June 15, 2026 at 6:17 pm

    You should specify cascade property of @ManyToOne annotation on address field of School class to save address before school. For example, CascadeType.ALL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just switched from XAMPP to MAMP and now this rewrite rule does not
I just switched from Eclipse to IntelliJ and noticed that when I use Reformat
So I just switched from some network solutions hosting running some php files that
I just switched my persistence.xml from <property name=toplink.ddl-generation value=drop-and-create-tables/> to <property name=toplink.ddl-generation value=create-tables/> and
I just recently switched from EF5 to NHibernate due to a few features that
Just switched to Mac from Windows and in ppt on Windows I had an
I just switched from Textmate 2 to Sublime Text 2. I figured that typing
We just switched from ojdbc14 to ojdbc6, and noticed that when we insert a
I just switched from C++ to Java. I'm trying to write a program that
I'm in a programming class where we have just switched from python to C.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.