How long are you immune to gastroenteritis after you have had it?

Define immune. There are dozens of gut viruses and some bacteria that could trigger gastroenteritis. You can develop long term immunity to one virus or strain but that will not prevent another virus or strain from triggering it at any time in the future.
No immunity. It can re-occur even if you had it in the past. Most common causes of gastroenteritis do not elicit protective immune response. Most common cause in USA is the Norovirus.
There are several. Bugs that can cause gastroenteritis. In short, if you get expose to the same one, you may have a much milder illness or no symptoms at all.